Can someone fix the errors in this C++ code?

Pages: 12
I started this code and there's some errors here's what i had to code. I had to write a program that stores a string containing a full name (First Middle Last) with a single space between any two successive names then it outputs each part of the name in a separate line. For testing purpose, your program MUST use the following name “Henry Louis Aaron” where the output in this case will as given below. Make sure that your program should function properly in case you use any other full name (it should be generic).

First Name: Henry
Middle Name: Louis
Last Name: Aaron
is what the output should look like.

and this is my code so far,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <string>
string first;
string middle;
string last;

cout<<"Enter your first name: ";
cin.getline(first);
cout<<"Enter your middle name: ";
cin.getline(middle);
cout<<"Enter your last name: ";
cin.getline(last);

string first="Henry";
string middle="Louis";
string last="Aaron";

string name;
name = first + " " + middle + " " + last;
cout << "Name is: " << name << endl;


can anyone help me fix the errors in the code so it looks like the correct output as shown above?
Last edited on
I'm not really sure what your asking. What do you mean about the name "Henry Louis Aaron"? Is that just a test name to see if the program works, but the user actually ought to be able to enter anything they want? And what exactly is the output supposed to be? That three line information table? Or is that the console with input on it?
im using Microsoft Visual studio i need to make a C++ code that can give me the output that i showed above when ran without debugging.
You didn't really answer my questions.
well im not really sure what your asking but, i guess its just a test, your susposed to make an output of

First Name: Henry
Middle Name: Louis
Last Name: Aaron
Well in that case, just modify your final cout<<"Name is: "<< /* etc */
line of code to print that instead of the name separated by spaces.
im confused could you tell me what goes where <</*ect*/ is??
actually i dont think that's the error theres a shit load of errors, could you copy my code into your visual basic as a C++ source file, and run it without debugging and tell me whats wrong with it?
You had the code there already; it displayed the names separated by spaces. Just change it to display "First:" "Middle:" and "Last:" in front of the respective variable.
i dont know what you mean im sorry, your gona have to bear with me, could you show me what you mean as a source code??
well i almost got it heres what i got

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
#include <string>
{
string first;
string middle;
string last;

cout<<"Enter your first name: ";
cin.getline(first);
cout<<"Enter your middle name: ";
cin.getline(middle);
cout<<"Enter your last name: ";
cin.getline(last);

string first="Henry";
string middle="Louis";
string last="Aaron";

string name;
name = first + " " + middle + " " + last;

cout << "First Name: " << strNames.substr(0,m);
cout << "\nMiddle Name:" << strNames.substr(m,l-m);
cout << "\nLast Name:" << strNames.substr(l) << endl;
cin.get();
}


theres only 1 error: error C2447: '{' : missing function header (old-style formal list?)
it says its on line 3, how do i fix this??
You are missing at int main() before the {

And I'm not really sure why you are going to all the trouble of concatenating those strings only to take substrs of them again, but oh well.

I don't really see how all of that would compile anyway, as you have multiple declarations of first, middle, and last.
well than what do i need to do to fix it? im really new at this stuff so idk how man lol, please help me
How did you even write all that if you are that new? Did you just copy it from somewhere? If so, I'd like to see the source.
i had a friend help me that far in and i cant figure out whats wrong with it, i didnt copy form anywhere
That's probably not helping your understanding. If you don't understand how to edit the code you have here, you probably need to write it again from scratch. It might seem like overkill, but it will help you figure out how much you know how to do. That's going to be important if you ever have to do this in the future.
well if anyone else is actally willing to help me and not give a lecture, i would appriciate that very much!!!
*Sigh* I see no reason to just give you answers so that you can pass a class. If you don't know enough to do this assignment, I don't understand why you think you deserve a good grade on it. You ought to get a grade based on whether or not you can do the work; clearly, you can't do the work. Thus, you will get a less desirable grade. If you have a question about how to do something specific, by all means, ask and we can assist you but we won't simply "do it" or "finish it."

Don't take this personally. It's how I feel, and pretty well aligned with the policies of this forum.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <string>
// you need to tell the compiler where the entry point is  int main()
//{
string first;
string middle;  // here you have your variables declared
string last;

cout<<"Enter your first name: ";
cin.getline(first);  // this works and you can also get inpout like  cin>>first;
cout<<"Enter your middle name: ";
cin.getline(middle);
cout<<"Enter your last name: ";
cin.getline(last);

string first="Henry";  //here you have the same variable declared twice
string middle="Louis"; // either get rid of all strings and make it   middle = "Louis"
string last="Aaron";  //or declare different variables like first1, or something else

string name;
name = first + " " + middle + " " + last;
cout << "Name is: " << name << endl;
// dont forget return 0;
//}  //everything else should compile, but I'm not debugging it for you 
Last edited on
its not compling right i keep getting errors about string being a undeclared identifier. :( on line 5
this is what i got now,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <string>
int main()
{
string first1;
string middle1;
string last1;

cout<<"Enter your first name: ";
cin>>first;  // this works and you can also get inpout like  cin>>first;
cout<<"Enter your middle name: ";
cin>>middle;
cout<<"Enter your last name: ";
cin>>last;

string first= "Henry";  //here you have the same variable declared twice
string middle = "Louis"; // either get rid of all strings and make it   middle = "Louis"
string last = "Aaron";  //or declare different variables like first1, or something else

string name;
name = first + " " + middle + " " + last;
cout << "Name is: " << name << endl;
return 0;
} 
Pages: 12