Vulxai Posted October 22, 2007 Share Posted October 22, 2007 I'm relearning C++. I'm trying just make a little program to start with. Here's what I have so far: #include using namespace std; int main() { int age; char firstName[20]; char middleName[20]; char lastName[20]; int number; number = 7; int number2; cout << "What is your age? "; cin >> age; cout << "What is your first name?"; cin >> firstName; cout << "What is your middle name?"; cin >> middleName; cout << "What is your last name?"; cin >> lastName cout >> "You are: " << age << " years old,"; cout >> " Your first name is: " << firstName << ","; cout >> " Your middle name is: " << middleName << ","; cout >> " Your last name is: " << lastName << ","; cout >> " Your first initial is: " << firstName[0] << ","; cout >> " Your middle initial is: " << middleName[0] << ","; cout >> " Your last initial is: " << lastName[0] << ","; cout >> " Guess a number between 1 and 10: "; cin >> number2; if (number2 == number) cout >> "You are right!"; if (number2 < number) cout >> "Nope, it's too low."; if (number2 > number) cout >> "Nope, it's too high."; return 0; } That will pop up and work and after you fill it in it'll say "Your are (whatever you typed in) years old, Your first name is (whatever you typed in), Your middle name is (whatever you typed in), Your last name is (whatever you typed in), Your first initial is (first letter of what you typed in), Your second initial is (first letter of whatever you typed in), Your last initial is (first letter of whatever you typed in), Guess a number between 1 and 10" then you type in a number and it'll show up as one of the three above. Now, my question. How can I make it so, for example if George Bush used it, it'll say "You are over 8 full hands years old, Your full name is George Dubbaya Bush, Your initials are GDB"and so on?I just had to make fun of him =) I just used him as an example if that's what you typed in. I tried this: #include using namespace std; int main() { int age; char firstName[20]; char middleName[20]; char lastName[20]; int number; number = 7; int number2; cout << "What is your age? "; cin >> age; cout << "What is your first name?"; cin >> firstName; cout << "What is your middle name?"; cin >> middleName; cout << "What is your last name?"; cin >> lastName cout >> "You are: " << age << " years old,"; cout >> " Your full name is: " << firstName; << " " middleName; << " " lastname; << ","; cout >> " Your initials are: " << firstName[0] << middleName[0] << lastName[0] << ","; cout >> " Guess a number between 1 and 10: "; cin >> number2; if (number2 == number) cout >> "You are right!"; if (number2 < number) cout >> "Nope, it's too low."; if (number2 > number) cout >> "Nope, it's too high."; return 0; } That comes back with these errors: error C2143: syntax error : missing ';' before '<<' (line 27) error C2143: syntax error : missing ';' before '<<' (line 27) error C2143: syntax error : missing ';' before '<<' (line 27) Line 27 is: cout >> " Your full name is: " << firstName; << " " middleName; << " " lastname; << ","; If I put the semi-colons in line 28 like I did line 27, it says the same thing, but it has line 28 also. So I take them out of line 28 and the errors for that line go away. So I take them out of line 27 and it gives me: error C2146: syntax error : missing ';' before identifier 'middleName' error C2296: '<<' : illegal, left operand has type 'char[20]' error C2296: '<<' : illegal, left operand has type 'char[20]' error C2297: '<<' : illegal, right operand has type 'const char [2]' error C2297: '<<' : illegal, right operand has type 'const char [2]' all for line 27. So can anyone help me out? ~ Proud Father ~ Proud (Currently Deployed) Army National Guardsmen ~ Proud Lakota ~ Retired Tip.It Crew ~ Link to comment Share on other sites More sharing options...
Nadril Posted October 22, 2007 Share Posted October 22, 2007 Fairly positive you don't put a ; after the variable, only at the end of the statement. Try that, at least, and see if it works. Its been a while since I've done C++. Link to comment Share on other sites More sharing options...
EWSentinel Posted October 22, 2007 Share Posted October 22, 2007 When using cout to show your results, only use one << per cout statement. Instead of using multiple couts, concatenate the results instead Example: cout << FirstName + ' ' + MiddleName + ' ' + LastName; KoAClan.org for adults Link to comment Share on other sites More sharing options...
Vulxai Posted October 22, 2007 Author Share Posted October 22, 2007 Fairly positive you don't put a ; after the variable, only at the end of the statement. Try that, at least, and see if it works. Its been a while since I've done C++. Already tried that, gave me: error C2146: syntax error : missing ';' before identifier 'middleName' error C2296: '<<' : illegal, left operand has type 'char[20]' error C2296: '<<' : illegal, left operand has type 'char[20]' error C2297: '<<' : illegal, right operand has type 'const char [2]' error C2297: '<<' : illegal, right operand has type 'const char [2]' When using cout to show your results, only use one >> per cout statement. Instead of using multiple couts, concatenate the results instead Example: cout << FirstName + ' ' + MiddleName + ' ' + LastName; error C2110: '+' : cannot add two pointers error C2110: '+' : cannot add two pointers error C2110: '+' : cannot add two pointers error C2146: syntax error: missing ';' before identifier 'lastName' error C2146: syntax error: missing ';' before identifier 'middleName' ~ Proud Father ~ Proud (Currently Deployed) Army National Guardsmen ~ Proud Lakota ~ Retired Tip.It Crew ~ Link to comment Share on other sites More sharing options...
Jard_Y_Dooku Posted October 23, 2007 Share Posted October 23, 2007 You just had some stream operators reversed, and a few semi-colons where they shouldn't be, and none where they should have been. Here's your complete, fixed code: #include using namespace std; int main() { int age; char firstName[20]; char middleName[20]; char lastName[20]; int number = 7; int number2; cout << "What is your age?" << endl; cin >> age; cout << "What is your first name?" << endl; cin >> firstName; cout << "What is your middle name?" << endl; cin >> middleName; cout << "What is your last name?" << endl; cin >> lastName; cout << endl; cout << "You are: " << age << " years old, "; cout << "your full name is: " << firstName << " " << middleName << " " << lastName << " and "; cout << "your initials are: " << firstName[0] << middleName[0] << lastName[0] << "!" << endl << endl; cout << "Guess a number between 1 and 10: "; cin >> number2; if (number2 == number) cout << "You are right!" << endl; if (number2 < number) cout << "Nope, it's too low." << endl; if (number2 > number) cout << "Nope, it's too high." << endl; cout << endl; return 0; } Never trust anyone. You are always alone, and betrayal is inevitable.Nothing is safe from the jaws of the decompiler. Link to comment Share on other sites More sharing options...
Brandon_7 Posted October 23, 2007 Share Posted October 23, 2007 Edit: Nevermind, Jard beat me to it. Link to comment Share on other sites More sharing options...
Vulxai Posted October 23, 2007 Author Share Posted October 23, 2007 Oooh, << instead of ;...that worked! Thanks! ~ Proud Father ~ Proud (Currently Deployed) Army National Guardsmen ~ Proud Lakota ~ Retired Tip.It Crew ~ Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now