0

I have a homework assignment I'm having a bit of trouble with. I seem to have no errors, but when I run the program and input text....nothing happens.

For example, to test it I usually put in "Robertson, Bob John" and hit enter. Can anyone help me with this?

#include <iostream>
#include <string> 

using namespace std;

int main () {

//Title and instructions
cout << "This program will output your name in first-to-last order!" << endl;
cout << "Please type your name in the following manner: last, first middle."; << endl;

//Declare the strings being used
string firstName;
string middleName;
string lastName;

//Put user input into strings, ignore the comma
cin >> lastName >> firstName >> middleName >> endl;
cin.ignore(',');

//Output the name in first-to-last order
cout << "Your name is: " << first <<' '<< middle <<' '<< last << endl;

//Pause before exiting
return 0;

}
2
  • 2
    This code does not compile. If you want our help, show us the real code. Commented Feb 24, 2014 at 5:47
  • 3
    //Pause before exiting return 0; - this is not a pause, but return code. Commented Feb 24, 2014 at 5:47

2 Answers 2

1

Since you say it compiles, presumably your real code doesn't have a rogue ; in the second cout line, doesn't try to read into endl, and uses the correct variable names in the final cout.

Assuming there are no other differences, the problem is this:

cin.ignore(',');

I'm not sure what you want that to do; but it waits until you enter an extra 44 characters (interpreting ',' as its ASCII value 44) after the name before proceeding.

If you want to ignore the comma after the last name, it's probably easiest to read it with the comma, then remove it with lastName.pop_back() (perhaps checking that there really is a comma there first).

By the way, my father has no middle name, and my sister has two. Are they not allowed to use your program?

Sign up to request clarification or add additional context in comments.

Comments

0

First, you have a couple of compilation errors; an extra semicolon on your second cout, wrong variable names when you're trying to cout your output, etc. But the real problem is your cin.ignore(',');. It seems to be hanging for some reason. I'm going to guess, based on the cin documentation, that it's interpreting the comma as a number, and it will ignore that many characters.

You'll need to remove the comma yourself after the cin; I've left that as an exercise for you.

#include <iostream>
#include <string>

using namespace std;

int main () {

//Title and instructions
cout << "This program will output your name in first-to-last order!" << endl;
cout << "Please type your name in the following manner: last, first middle." << endl;

//Declare the strings being used
string firstName;
string middleName;
string lastName;

//Put user input into strings, ignore the comma
cin >> lastName >> firstName >> middleName;

//Output the name in first-to-last order
cout << "Your name is: " << firstName <<' '<< middleName <<' '<< lastName << endl;

//Pause before exiting
return 0;

}

3 Comments

Ah, I copied some older code by accident. Thank you for the answer, helped me a lot. Literally my third assignment haha.
@user3345181 No problem. We've all got to start somewhere! ^_^ Don't forget to accept my answer by checking the little checkmark next to it, that will give us both some reputation.
@user3345181 Also, here are a few sites you can learn from and look things up on. They should help you along your journey. cplusplus.com and cppreference.com

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.