Skip to content
View in the app

A better way to browse. Learn more.

Tip.It Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

C++ code help

Featured Replies

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 ~
 

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++.

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;

Ewsentinel.png

KoAClan.org for adults

  • Author
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 ~
 

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.

Edit: Nevermind, Jard beat me to it.

  • Author

Oooh, << instead of ;...that worked! Thanks!

~ Proud Father ~ Proud (Currently Deployed) Army National Guardsmen ~ Proud Lakota ~ Retired Tip.It Crew ~
 

Create an account or sign in to comment

Important Information

By using this site, you agree to our Terms of Use.

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.