Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

unknown.exe

macrumors member
Original poster
Sep 22, 2007
67
0
Somewhere on Earth
Sup, I'm writing a program that sort of combines all of the programs from my computer class into one big program, along with other programs I have invented. I was just wondering if anyone had any suggestions for my program. Like any programs that I can add, any way to make my program more efficient... etc. I'm sort of looking for stuff to add eg. a password system(which I have). I only request that you do not point out that goto operations are bad... they are staying;). Anyone that helps me out with this program will get recognition on my site and in my program (not much of an incentive, but you should get a war, fuzzy feeling inside for helping out).
Here is my code (PS: The ASCII banners are screwed up because of the copy and paste... just try to look around them:)):apple:
//ALL-IN-ONE PROGRAM DESIGNED BY CONNOR SPANGLER
//LAST UPDATED 11/29/07
//VERSION - 2.0
//TO DO LIST: INCLUDE WEIGHT CONVERSION PROG AND CURRENCY CONVERSION PROG...
#include <iostream.h>
#include <stdlib.h>
#include <time.h>
#include <stdio.h>
#include <string.h>

int main()
{
{
int password;
int numtries = 0;
{
cout << "PLEASE TYPE IN THE PASSWORD...\nPASSWORD: ";
password:
cin >> password;
cerr << "LOGGING IN";
sleep(1);
cerr << ".";
sleep(1);
cerr << ".";
sleep(1);
cerr << ".\n";
sleep(1);
if (password == 2090407)
{
cout << "PASSWORD VERIFIED...\n";
goto verystart;
}
else
{
numtries = numtries + 1;
if (numtries == 5)
{
cout << "PASSWORD DENIED...\nSYSTEM LOGGING OUT...\n";
return 0;
}
}
cout << "PASSWORD DENIED, PLEASE TRY AGAIN...\n";
goto password;
}
{
verystart:
cout << " WELCOME TO THE\n";
cout << " ### #### ####### ######## ######## ####### ###### \n";
cout << " ## ## ## ## ## ## ## ## ## ## ## ## ## \n";
cout << " ## ## ## ## ## ######## ######## ## ## ## \n";
cout << " ######### ## ## ## ## ## ## ## ## ## #### \n";
cout << " ## ## ## ## ## ## ## ## ## ## ## ## \n";
cout << " ## ## #### ####### ## ## ## ####### ###### \n";
restart:
cout << "\n";
cout << "TYPE 1 FOR THE NUCLEAR PROGRAM...\n";
cout << "TYPE 2 FOR THE TRANSPORTATION PROGRAM...\n";
cout << "TYPE 3 FOR THE SPEED TICKET PROGRAM...\n";
cout << "TYPE 4 FOR THE SURVEYING PROGRAM...\n";
cout << "TYPE 5 FOR THE SALES TAX PROGRAM...\n";
cout << "TYPE 6 FOR THE BOX VOLUME PROGRAM...\n";
cout << "TYPE 7 FOR THE RESTAURANT PROGRAM...\n";
cout << "TYPE 8 FOR THE ROCK PAPER SCISSORS PROGRAM...\n";
cout << "TYPE 9 FOR THE PROGRAM MANAGER PROGRAM...\n";
cout << "TYPE 10 TO QUIT THE PROGRAM...\n";
start:
int choice = 0;
cin >> choice;
if (choice == 1)
goto nuke;
if (choice == 2)
goto trans;
if (choice == 3)
goto speed;
if (choice == 4)
goto survey;
if (choice == 5)
goto sales;
if (choice == 6)
goto volbox;
if (choice == 7)
goto food;
if (choice == 8)
goto rps;
if (choice == 9)
goto progm;
if (choice == 10)
goto end;
else
cout << "SORRY, THAT IS NOT ONE OF THE CHOICES... PLEASE TRY AGAIN.\n";
goto start;
}
{
//NUCLEAR PROGRAM
double numberofnukes;
double totalprice;
char country [10];
char place [20];
char typeofnuke [30];
nuke:
cerr << " WELCOME TO THE NUKES R' US\n";
cout << " ## ## ## ## ###### ## ######## ### ######## \n";
cout << " ### ## ## ## ## ## ## ## ## ## ## ## \n";
cout << " #### ## ## ## ## ## ## ## ## ## ## \n";
cout << " ## ## ## ## ## ## ## ###### ## ## ######## \n";
cout << " ## #### ## ## ## ## ## ######### ## ## \n";
cout << " ## ### ## ## ## ## ## ## ## ## ## ## \n";
cout << " ## ## ####### ###### ######## ######## ## ## ## ## \n";
cout << " \n";
cout << " ######## ######## ####### ###### ######## ### ## ## \n";
cout << " ## ## ## ## ## ## ## ## ## ## ## ## ### ### \n";
cout << " ## ## ## ## ## ## ## ## ## ## ## #### #### \n";
cout << " ######## ######## ## ## ## #### ######## ## ## ## ### ## \n";
cout << " ## ## ## ## ## ## ## ## ## ######### ## ## \n";
cout << " ## ## ## ## ## ## ## ## ## ## ## ## ## \n";
cout << " ## ## ## ####### ###### ## ## ## ## ## ## \n";
cerr << "What type of nuke would you like:\n";
cerr << "A mega, ultra, super, or grande bomb?\n";
cin >> typeofnuke;
cerr << "You have chosen a "<< typeofnuke <<" bomb!\n";
cerr << "Enter the number of " << typeofnuke << " nukes you would like to purchase: ";
cin >> numberofnukes;
totalprice = (numberofnukes * 20) + (numberofnukes * 20) * .05;
cerr << "Your total will be $" << totalprice << " with shipping and handling.\n";
cerr << "Would you like to have your nuke launched directly to a country?\n";
cin >> country;
if (strcmp( country, "yes") == 0)
{
cout << "Ok, which country?\n";
cin >> place;
}
else
{
cout << "OK, your nukes will be FedExed to you. They should arrive within one buisness day. Thank you!\n";
goto restart;
}
cerr << "Your nuke has been sent to... " << place << "!!!\n";
cerr << "The nuke will hit " << place << " in 10 seconds...\n";
sleep(1);
cerr << "10\n";
sleep(1);
cerr << "9\n";
sleep(1);
cerr << "8\n";
sleep(1);
cerr << "7\n";
sleep(1);
cerr << "6\n";
sleep(1);
cerr << "5\n";
sleep(1);
cerr << "4\n";
sleep(1);
cerr << "3\n";
sleep(1);
cerr << "2\n";
sleep(1);
cerr << "1\n";
sleep(1);
cerr << "0\n";
sleep(1);
srand ( time(NULL) );
printf ("%d people dead \n", rand() % 9102785);
srand ( 1 );
cerr << "Have a nice armaggedon!\n";
goto restart;
}
{
//TRANSPORTATION
trans:
cout << " ######## ######## ### ## ## ###### #### ######## \n";
cout << " ## ## ## ## ## ### ## ## ## ## ## \n";
cout << " ## ## ## ## ## #### ## ## ## ## \n";
cout << " ## ######## ## ## ## ## ## ###### ## ## \n";
cout << " ## ## ## ######### ## #### ## ## ## \n";
cout << " ## ## ## ## ## ## ### ## ## ## ## \n";
cout << " ## ## ## ## ## ## ## ###### #### ## \n";
cout << " \n";
cout << " ######## ######## ####### ###### ######## ### ## ## \n";
cout << " ## ## ## ## ## ## ## ## ## ## ## ## ### ### \n";
cout << " ## ## ## ## ## ## ## ## ## ## ## #### #### \n";
cout << " ######## ######## ## ## ## #### ######## ## ## ## ### ## \n";
cout << " ## ## ## ## ## ## ## ## ## ######### ## ## \n";
cout << " ## ## ## ## ## ## ## ## ## ## ## ## ## \n";
cout << " ## ## ## ####### ###### ## ## ## ## ## ## \n";
int numvan = 0;
int numpeople = 0;
int numleftover = 0;
cout << "Each bus holds 50 people. \n";
cout << "Each van holds 5 people. \n";
cout << "Enter the number of people that need a ride: \n";
cin >> numpeople;
numleftover = numpeople % 50;
numvan =numleftover / 5;
cout << "The number of buses needed = " << numpeople / 50 << "\n";
cout << "The number of vans needed = " << numvan + 1 << "\n";
goto restart;
}
{
//SPEED TICKET
speed:
cout << " ######## #### ###### ## ## ######## ######## \n";
cout << " ## ## ## ## ## ## ## ## \n";
cout << " ## ## ## ## ## ## ## \n";
cout << " ## ## ## ##### ###### ## \n";
cout << " ## ## ## ## ## ## ## \n";
cout << " ## ## ## ## ## ## ## ## \n";
cout << " ## #### ###### ## ## ######## ## \n";
cout << " \n";
cout << " ######## ######## ####### ###### ######## ### ## ## \n";
cout << " ## ## ## ## ## ## ## ## ## ## ## ## ### ### \n";
cout << " ## ## ## ## ## ## ## ## ## ## ## #### #### \n";
cout << " ######## ######## ## ## ## #### ######## ## ## ## ### ## \n";
cout << " ## ## ## ## ## ## ## ## ## ######### ## ## \n";
cout << " ## ## ## ## ## ## ## ## ## ## ## ## ## \n";
cout << " ## ## ## ####### ###### ## ## ## ## ## ## \n";
long int speedcar = 0;
const int FINE = 100;
long int totalfine = 0;
cout << "The speed limit is 60 mph, enter the speed of the car: \n";
cin >> speedcar;
totalfine = FINE + ((speedcar - 60) * 10);
cout << "The total fine is $" << totalfine << ".\n";
goto restart;
}
{
//SURVEYING
survey:
cout << " ## ## ######## ### ###### ## ## ######## ########\n";
cout << " ### ### ## ## ## ## ## ## ## ## ## ## \n";
cout << " #### #### ## ## ## ## ## ## ## ## ## \n";
cout << " ## ### ## ###### ## ## ###### ## ## ######## ###### \n";
cout << " ## ## ## ######### ## ## ## ## ## ## \n";
cout << " ## ## ## ## ## ## ## ## ## ## ## ## \n";
cout << " ## ## ######## ## ## ###### ####### ## ## ########\n";
cout << " \n";
cout << " ######## ######## ####### ###### ######## ### ## ## \n";
cout << " ## ## ## ## ## ## ## ## ## ## ## ## ### ### \n";
cout << " ## ## ## ## ## ## ## ## ## ## ## #### #### \n";
cout << " ######## ######## ## ## ## #### ######## ## ## ## ### ## \n";
cout << " ## ## ## ## ## ## ## ## ## ######### ## ## \n";
cout << " ## ## ## ## ## ## ## ## ## ## ## ## ## \n";
cout << " ## ## ## ####### ###### ## ## ## ## ## ## \n";
double temperature = 0.0;
double measuredL = 0.0;
double correction = 0.0;
double newlength;
cout << "Input the current tempurature in Fahrenheit:\n";
cin >> temperature;
cout << "Input the length you measured in inches:\n";
cin >> measuredL;
correction = 0.0000116 * (temperature - 20.0) * measuredL;
newlength = measuredL + correction;
cout << "The correct measured length is " << newlength << ".\n";
goto restart;
}
{
//SALES TAX
sales:
cout << " ####### ### ## ######## ###### \n";
cout << " ## ## ## ## ## ## ## ## \n";
cout << " ## ## ## ## ## ## \n";
cout << " ###### ######### ## ###### ###### \n";
cout << " ## ## ## ## ## ## \n";
cout << " ## ## ## ## ## ## ## ## \n";
cout << " ###### ## ## ######## ######## ###### \n";
cout << " \n";
cout << " ######## ######## ####### ###### ######## ### ## ## \n";
cout << " ## ## ## ## ## ## ## ## ## ## ## ## ### ### \n";
cout << " ## ## ## ## ## ## ## ## ## ## ## #### #### \n";
cout << " ######## ######## ## ## ## #### ######## ## ## ## ### ## \n";
cout << " ## ## ## ## ## ## ## ## ## ######### ## ## \n";
cout << " ## ## ## ## ## ## ## ## ## ## ## ## ## \n";
cout << " ## ## ## ####### ###### ## ## ## ## ## ## \n";
float priceitem = 0.0;
float salestax = 0.0;
float totalcost = 0.0;
cout << "What is the sales tax where you liv (ex: .07, .08 etc.):\n";
cin >> salestax;
cout << "What is the price of the item you would like to buy:\n";
cin >> priceitem;
totalcost = priceitem + (priceitem * salestax);
cout << "The total price of your item is " << totalcost << ".\n";
goto restart;
}
{
//BOX VOLUME
volbox:
cout << " ## ## ####### ## ## ## ## ## ######## \n";
cout << " ## ## ## ## ## ## ## ### ### ## \n";
cout << " ## ## ## ## ## ## ## #### #### ## \n";
cout << " ## ## ## ## ## ## ## ## ### ## ###### \n";
cout << " ## ## ## ## ## ## ## ## ## ## \n";
cout << " ## ## ## ## ## ## ## ## ## ## \n";
cout << " ### ####### ######## ####### ## ## ######## \n";
cout << " \n";
cout << " ######## ######## ####### ###### ######## ### ## ## \n";
cout << " ## ## ## ## ## ## ## ## ## ## ## ## ### ### \n";
cout << " ## ## ## ## ## ## ## ## ## ## ## #### #### \n";
cout << " ######## ######## ## ## ## #### ######## ## ## ## ### ## \n";
cout << " ## ## ## ## ## ## ## ## ## ######### ## ## \n";
cout << " ## ## ## ## ## ## ## ## ## ## ## ## ## \n";
cout << " ## ## ## ####### ###### ## ## ## ## ## ## \n";
int sidea = 0;
int sideb = 0;
int sidec = 0;
int totalvol = 0;
char units [10];
cout << "What are you measuring in (inches, feet, yards, etc.)?\n";
cin >> units;
cout << "How many " << units << " is the first side?\n";
cin >> sidea;
cout << "How many " << units << " is the second side?\n";
cin >> sideb;
cout << "How many " << units << " is third side?\n";
cin >> sidec;
totalvol = sidea * sideb * sidec;
cout << "The volume of the box is " << totalvol << " " << units <<" cubed.\n";
goto restart;
}
{
//RESTAURANT
food:
cout << " ######## ### ###### ######## ######## ####### ####### ########\n";
cout << " ## ## ## ## ## ## ## ## ## ## ## ## ## \n";
cout << " ## ## ## ## ## ## ## ## ## ## ## ## \n";
cout << " ###### ## ## ###### ## ###### ## ## ## ## ## ## \n";
cout << " ## ######### ## ## ## ## ## ## ## ## ## \n";
cout << " ## ## ## ## ## ## ## ## ## ## ## ## ## \n";
cout << " ## ## ## ###### ## ## ####### ####### ####### \n";
cout << " \n";
cout << " ######## ######## ####### ###### ######## ### ## ## \n";
cout << " ## ## ## ## ## ## ## ## ## ## ## ## ### ### \n";
cout << " ## ## ## ## ## ## ## ## ## ## ## #### #### \n";
cout << " ######## ######## ## ## ## #### ######## ## ## ## ### ## \n";
cout << " ## ## ## ## ## ## ## ## ## ######### ## ## \n";
cout << " ## ## ## ## ## ## ## ## ## ## ## ## ## \n";
cout << " ## ## ## ####### ###### ## ## ## ## ## ## \n";
int hamburgers = 0;
int fries = 0;
int sodas = 0;
char typesodas [10];
int cookies = 0;
char typecookies [10];
int appleturnovers = 0;
int salads = 0;
double foodprice = 0;
cout << " MENU:\n";
cout << "Hamburger------------------ $2.00\n";
cout << "Soda----------------------- $1.00\n";
cout << "Cookie--------------------- $1.00\n";
cout << "Apple-turnover------------- $1.75\n";
cout << "Salad---------------------- $1.50\n";
cout << "How many hamburgers would you like?\n";
cin >> hamburgers;
cout << "How many fries would you like?\n";
cin >> fries;
cout << "How many sodas would you like?\n";
cin >> sodas;
cout << "What kind of soda?\n";
cin >> typesodas;
cout << "How many cookies would you like?\n";
cin >> cookies;
cout << "What type of cookie would you like?\n";
cin >> typecookies;
cout << "How many apple-turnovers would you like?\n";
cin >> appleturnovers;
cout << "How many salads would you like?\n";
cin >> salads;
cout << "You have ordered: " << hamburgers << " hamburgers, " << fries << " fries, " << sodas << " " << typesodas << "s, " << cookies << " " << typecookies << " cookies, " << appleturnovers << " appleturnovers, and " << salads << " salads.\n";
foodprice = (hamburgers * 2.00) + (fries * 1.00) + (sodas * 1.00) + (cookies * 1.00) + (appleturnovers * 1.75) + (salads * 1.50);
cout << "Your total will be $" << foodprice << ". Thank you!\n";
goto restart;
}
{
//Rock, Paper, Scissors
rps:
cout << " ######## ####### ###### ## ## \n";
cout << " ## ## ## ## ## ## ## ## \n";
cout << " ## ## ## ## ## ## ## \n";
cout << " ######## ## ## ## ##### \n";
cout << " ## ## ## ## ## ## ## \n";
cout << " ## ## ## ## ## ## ## ## \n";
cout << " ## ## ####### ###### ## ## \n";
cout << " \n";
cout << " ######## ### ######## ######## ######## \n";
cout << " ## ## ## ## ## ## ## ## ## \n";
cout << " ## ## ## ## ## ## ## ## ## \n";
cout << " ######## ## ## ######## ###### ######## \n";
cout << " ## ######### ## ## ## ## \n";
cout << " ## ## ## ## ## ## ## \n";
cout << " ## ## ## ## ######## ## ## \n";
cout << " \n";
cout << " ###### ###### #### ###### ###### ####### ######## ###### \n";
cout << " ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## \n";
cout << " ## ## ## ## ## ## ## ## ## ## \n";
cout << " ###### ## ## ###### ###### ## ## ######## ###### \n";
cout << " ## ## ## ## ## ## ## ## ## ## \n";
cout << " ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## \n";
cout << " ###### ###### #### ###### ###### ####### ## ## ###### \n";
rpstart:
int choice;
int compguess;
const int ROCK = 1;
const int PAPER = 2;
const int SCISSORS = 3;
{
srand((unsigned)time(0));
for(int index=0; index<1; index++)
compguess = (rand()%3)+1;
}
cout << "Type 1 for Rock, 2 for Paper, or 3 for Scissors...\n";
cin >> choice;
switch (choice)
{
case ROCK:
if (compguess == ROCK)
cout << "You = Rock Computer = Rock\nIt's a tie!\n";
else if (compguess == PAPER)
cout << "You = Rock Computer = Paper\nComputer wins!\n";
else
cout << "You = Rock Computer = Scissors\nYou win!\n";
break;
case PAPER:
if (compguess == ROCK)
cout << "You = Paper Computer = Rock\nYou win!!\n";
else if (compguess == PAPER)
cout << "You = Paper Computer = Paper\nIt's a tie!\n";
else
cout << "You = Paper Computer = Scissors\nComputer wins!\n";
break;
case SCISSORS:
if (compguess == ROCK)
cout << "You = Scissors Computer = Rock\nComputer wins!\n";
else if (compguess == PAPER)
cout << "You = Scissors Computer = Paper\nYou win!\n";
else
cout << "You = Scissors Computer = Scissors\nIt's a tie!\n";
break;
default:
cout << "That's not a choice, please try again...\n";
goto rpstart;
}
goto restart;
}
{
progm:
cout << " ######## ######## ####### ###### ######## ### ## ## \n";
cout << " ## ## ## ## ## ## ## ## ## ## ## ## ### ### \n";
cout << " ## ## ## ## ## ## ## ## ## ## ## #### #### \n";
cout << " ######## ######## ## ## ## #### ######## ## ## ## ### ## \n";
cout << " ## ## ## ## ## ## ## ## ## ######### ## ## \n";
cout << " ## ## ## ## ## ## ## ## ## ## ## ## ## \n";
cout << " ## ## ## ####### ###### ## ## ## ## ## ## \n";
cout << " ## ## ### ## ## ### ###### ######## ######## \n";
cout << " ### ### ## ## ### ## ## ## ## ## ## ## ## \n";
cout << " #### #### ## ## #### ## ## ## ## ## ## ## \n";
cout << " ## ### ## ## ## ## ## ## ## ## ## #### ###### ######## \n";
cout << " ## ## ######### ## #### ######### ## ## ## ## ## \n";
cout << " ## ## ## ## ## ### ## ## ## ## ## ## ## \n";
cout << " ## ## ## ## ## ## ## ## ###### ######## ## ##\n";
int decision;
choicea:
cout << "TYPE IN THE NUMBER OF THE PROGRAM YOU WOULD LIKE TO OPEN...\n";
cout << "1 = FIREFOX 2 = ICHAT 3 = FLIGHTGEAR 4 = VLC 5 = ITUNES\n";
cin >> decision;
switch (decision)
{
case 1:
system("open /Applications/Firefox.app");
break;
case 2:
system("open /Applications/iChat.app");
break;
case 3:
system("open /Applications/Flightgear/Flightgear.app");
break;
case 4:
system("open /Applications/VLC.app");
break;
case 5:
system("open /Applications/iTunes.app");
break;
default:
{
cout << "SORRY, THAT IS NOT ONE OF THE OPTIONS...\nPLEASE CHOOSE A VALID CHOICE...\n";
goto choicea;
}
}
goto restart;
}
}
{
end:
cerr << "SYSTEM LOGGING OUT";
sleep(1);
cerr << ".";
sleep(1);
cerr << ".";
sleep(1);
cerr << ".\n";
sleep(1);
return 0;
}
}
 

Cromulent

macrumors 604
Oct 2, 2006
6,810
1,100
The Land of Hope and Glory
No offence but your code looks horrible.

Where are your functions? Being able to write something that works is all very well, but if you are planning on coding for a profession you should at least try and stick to convention and try not to get into bad habits from the start.

Did the forum post get rid of your indentation or do you not have any?
 

pilotError

macrumors 68020
Apr 12, 2006
2,237
4
Long Island
The forum does that...

The OP needs to put the source code between a code tag and a /code tag (in square brackets of course) so it maintains all the formatting.

Not sure if this works on a Mac, but try:

Code:
#include <pwd.h>
#include <unistd.h>
#include <stdio.h>

static char *password;

int main()
{
    password = getpass("Password: ");
    printf("%s\n",password);
}

The other way to do it would be to get the characters and either echo a character back or don't echo at all.

Code:
char password[20];  // Accept up to 20 char password 

for (i=0; i<20; i++)
{

   password[i] = getch();
   cout << "*";

   if (ch[i]==13)  // should this be a 10?  I forget my CR's from my LF's...
      checkPassword();
}
 

Gelfin

macrumors 68020
Sep 18, 2001
2,165
5
Denver, CO
No, seriously, goto operations are bad, and yours are worse than most. You've come closer than I've ever seen to unintentionally making C look like Apple II BASIC. It's like you're baking pies filled with Play-Doh and saying, "don't tell me not to use Play-Doh because it's all I have." The fact it's all you have doesn't make it food.

Learn how to work with functions NOW. This is not a case of somebody trying to make you conform to an arbitrary rule. This is something you really, seriously need to take care of ASAP.
 

unknown.exe

macrumors member
Original poster
Sep 22, 2007
67
0
Somewhere on Earth
The other way to do it would be to get the characters and either echo a character back or don't echo at all.

Code:
char password[20];  // Accept up to 20 char password 

for (i=0; i<20; i++)
{

   password[i] = getch();
   cout << "*";

   if (ch[i]==13)  // should this be a 10?  I forget my CR's from my LF's...
      checkPassword();
}

Uuum, I'm pretty sure you are telling me how to replace the password output w/ asteriks in this code, but I'm programming in C++ and I think this is C... and when I try to compile it it says that "if (ch==13)" is incorrect because of the ch, and checkPassword doesn't exist. Also, in the for statement, i = 0... i isn't declared, but when I declare it the error goes away. Can you please give me a little more info/help on the asterik replacement problem?:apple:
 

itickings

macrumors 6502a
Apr 14, 2007
947
185
Uuum, I'm pretty sure you are telling me how to replace the password output w/ asteriks in this code, but I'm programming in C++ and I think this is C... and when I try to compile it it says that "if (ch==13)" is incorrect because of the ch, and checkPassword doesn't exist. Also, in the for statement, i = 0... i isn't declared, but when I declare it the error goes away. Can you please give me a little more info/help on the asterik replacement problem?:apple:


I know you requested us not to complain about the goto functions, but seriously, the first thing to do with that program if you want to make it better - use functions, remove gotos. Anyway, since you obviously intend to keep the gotos, you shouldn't complain about a C-solution. C++ is a superset of C, so the code will work fine after fixing the errors. If you want to claim you're coding in C++, get rid of the gotos.

As for the errors, you're supposed to write the function checkPassword yourself ;), and ch is simply misnamed. The code isn't perfect, it has some other flaws too but I'm pretty sure the intent was to point you in a direction where you can solve the problem yourself, not doing all the work for you.
 

ledd

macrumors newbie
Dec 26, 2006
23
0
Uuum, I'm pretty sure you are telling me how to replace the password output w/ asteriks in this code, but I'm programming in C++ and I think this is C... and when I try to compile it it says that "if (ch==13)" is incorrect because of the ch, and checkPassword doesn't exist. Also, in the for statement, i = 0... i isn't declared, but when I declare it the error goes away. Can you please give me a little more info/help on the asterik replacement problem?:apple:


What I believe pilotError is doing here is checking for the "enter" char as in the user has pressed enter (there password entry is complete). Carriage return is 13 so you would I believe want
if(password == 13)
{
checkPassword();
}
 

ledd

macrumors newbie
Dec 26, 2006
23
0
pilotError, getpass() works sweet. Never knew about that one. Learn something new everyday. Thanks.
 

unknown.exe

macrumors member
Original poster
Sep 22, 2007
67
0
Somewhere on Earth
The forum does that...
The other way to do it would be to get the characters and either echo a character back or don't echo at all.

Code:
char password[20];  // Accept up to 20 char password 

for (i=0; i<20; i++)
{

   password[i] = getch();
   cout << "*";

   if (ch[i]==13)  // should this be a 10?  I forget my CR's from my LF's...
      checkPassword();
}

Ok i've eddited this code a bit and I have ended up with this:
Code:
#include <iostream.h>
#include <stdlib.h>
#include <time.h>
#include <stdio.h>
#include <string.h>
#include <windows.h>
#include <conio.h>

int PassCheck(char password2[]);
void main()
{
	char password[20]; 
    for (int i=0; i<20; i++)
	{
       password[i] = getch();
       cerr << "*";
       if(password[i] == 9) 
       PassCheck(password);
	}
}
int PassCheck(char password2[])
{
	int check;
	int numtries = 0;
	cerr << "PLEASE TYPE IN THE PASSWORD...\nPASSWORD: ";
	cin >> password2;
	cerr << "LOGGING IN";
	Sleep(1000);
	cerr << ".";
	Sleep(1000);
	cerr << ".";
	Sleep(1000);
	cerr << ".\n";
	Sleep(1000);
	strcmp (password2, "dudesons");
	check = 1;
	if (check == 1)
	{  
	   cerr << "PASSWORD VERIFIED...\n";
	   cout << "goto op here lol";
	}
	else
	{
	numtries = numtries + 1;
	if (numtries == 5)
      {
	  cerr << "SYSTEM LOGGING OUT...\n";
      return 0;
	  }
	}
	cerr << "PASSWORD DENIED, PLEASE TRY AGAIN...\n";
	return 0;;
}
This code should do the following:
-Ask you for the password
-Replace password you type with asterisks (*) as you type
-Check if the password is correct
-If it is incorrect, see if the incorrect pass has been typed 5 times (if so then it exits, if not then it will ask for the pass again but i will add that later; for now it just exits if the pass is bad).

The code compiles and runs without errors but when it runs it shows a blank compiler screen until you type something... then EVERYTHING you press on the keyboard turns into asteriks. It does this up to 19 asteriks, where the program ends because the variable for password allows 19 characters and a null.
Can someone please help me with this code?!?!?!? I've been working on it for months!!!
I WILL WORSHIP WHOEVER CAN HELP ME MAKE THIS WORK!!!!!:D
Thanks in advance!:apple:
 

themoonisdown09

macrumors 601
Nov 19, 2007
4,319
18
Georgia, USA
I rewrote the main function for you. The password must be 20 characters or less and it checks the password when the user presses the ENTER key. You can change "printf" to "cout" if that's what you like better. I hope this is what you were looking for...

Code:
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>

void main()
{
	char password[20];
	char input = '\0';
	int counter = 0;

	printf("Please enter your password: ");

	// password must be 20 characters or less
	while (counter < 20)
	{
		// get user input
		input = getch();

		// stop when user presses the ENTER key
		if (input == 13)
		{
			// end the string
			password[counter] = '\0';
			break;
		}

		// add input to password string
		password[counter] = input;

		// display * instead of character
		printf("*");

		// increment character count
		counter++;
	}

	// check the password
	PassCheck(password);
}
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.