-
September 21st, 2012, 11:16 AM
#1
nter first and surname and then display the full name problem
Hi
I'am new to C++, i done various basic programs in C++, but one program is giving me a major headache: enter first and surname and then display the full name:
code below:
// Enter first and surname and then display the full name.
#include<iostream>
#include<string>
#include<cctype>
using namespace std;
int main()
{
string first_name, surname;
int main()
{
inp_string
Pr_string
return 0;
}
void inp_string
{
cout << "Enter your first_name";
// cin >> first name;
getline(cin,first_name); // reads all the line plus spaces
cout << "enter your surname";
// cin >> surname;
getline(cin, suranme); // reads all the line plus spaces
system ("CLS");
}
void pr_string
{
cout << "My full name is " << name << "\n";
}
-
September 21st, 2012, 11:49 AM
#2
Re: nter first and surname and then display the full name problem
Here's the code with code tags (please use code tags in the future):
Code:
// Enter first and surname and then display the full name.
#include<iostream>
#include<string>
#include<cctype>
using namespace std;
int main()
{
string first_name, surname;
int main()
{
inp_string
Pr_string
return 0;
}
void inp_string
{
cout << "Enter your first_name";
// cin >> first name;
getline(cin,first_name); // reads all the line plus spaces
cout << "enter your surname";
// cin >> surname;
getline(cin, suranme); // reads all the line plus spaces
system ("CLS");
}
void pr_string
{
cout << "My full name is " << name << "\n";
}
Now, there is a lot wrong with your program. As it stands it will not compile. You reference two main functions. You aren't calling your functions correctly. Even if you were calling them correctly:
Code:
inp_string();
pr_string();
That would still not work. You need to declare your functions before using them. You can then define them after main() like you have now.
So:
Code:
void inp_string();
void pr_string();
int main()
{
inp_string();
pr_string();
return 0;
}
void inp_string()
{
//definition here
}
void pr_string()
{
//definition here
}
On top of that, you have a spelling mistake in inp_string(). In pr_string() you have an undefined variable (name). At this point I would probably start over. If you are using a textbook, reread the sections on functions and input.
-
September 21st, 2012, 11:56 AM
#3
Re: nter first and surname and then display the full name problem
ok i understabd a little bit, if you were to right the same program, what would your code be?
-
September 21st, 2012, 12:03 PM
#4
Re: nter first and surname and then display the full name problem
It is again forum policy for us to do your homework. If I posted what I would write, then you would have the answer and not learn anything.
-
September 21st, 2012, 02:03 PM
#5
Re: enter first and surname and then display the full name problem
Ok made some changes when i press ctrl and f5 runs program asks for my first_name then surname then i press enter system says name isfirst_namesurname
PHP Code:
// Enter first and surname and then display the full name.
#include<iostream>
#include<string>
#include<cctype>
using namespace std;
string first_name, surname, name;
void in_string();
void pr_string();
int main()
{
in_string();
pr_string();
return 0;
}
void in_string()
{
cout << "enter your first_name";
// cin >> first name;
getline(cin,first_name); // reads all the line plus spaces
cout << "enter your surname";
// cin >> surname;
getline(cin, surname); // // reads all the line plus spaces
system ("CLS");
}
void pr_string()
{
cout << "name is" << "first_name" << "surname" << name << "\n";
}
-
September 21st, 2012, 02:26 PM
#6
Re: enter first and surname and then display the full name problem
ok it works now only one wee minor problem, when you enter your first name then enter surname enter the full name is displayed, but no space in between first and surname, example johnsmith should be john smith with a space in the middle
PHP Code:
// Enter first and surname and th2; display the full name.
#include<iostream>
#include<string>
#include<cctype>
using namespace std;
string first_name, surname, name;
void in_string();
void pr_string();
int main()
{
in_string();
pr_string();
return 0;
}
void in_string()
{
cout << "enter your first_name: ";
// cin >> first name;
getline(cin,first_name); // reads all the line plus spaces
cout << "enter your surname: ";
// cin >> surname;
getline(cin, surname); // // reads all the line plus spaces
system ("CLS");
}
void pr_string()
{
cout << "name is " << first_name << surname << name << "\n"
}
-
September 21st, 2012, 02:47 PM
#7
Re: nter first and surname and then display the full name problem
still can not get space in between the first and surname
PHP Code:
// Enter first and surname and then display the full name.
#include<iostream>
#include<string>
#include<cctype>
using namespace std;
string first_name, surname, name;
void in_string();
void pr_string();
int main()
{
in_string();
pr_string();
return 0;
}
void in_string()
{
cout << "enter your first_name: ";
// cin >> first name;
getline(cin, first_name); // reads all the line plus spaces
cout << "enter your surname: ";
// cin >> surname;
getline(cin, surname); // // reads all the line plus spaces
system ("CLS");
}
void pr_string()
{
cout << "my name is " << first_name << surname << name << "\n";
}
-
September 21st, 2012, 03:08 PM
#8
Re: nter first and surname and then display the full name problem
Problem fixed last line
PHP Code:
void pr_string()
{
cout << "my name is " << first_name << " " << surname << name << "\n";
}
final code
PHP Code:
// Enter first and surname and then display the full name.
#include<iostream>
#include<string>
#include<cctype>
using namespace std;
string first_name, surname, name;
void in_string();
void pr_string();
int main()
{
in_string();
pr_string();
return 0;
}
void in_string()
{
cout << "enter your first_name: ";
// cin >> first name;
getline(cin,first_name); // reads all the line plus spaces
cout << "enter your surname: ";
// cin >> surname;
getline(cin,surname); // reads all the line plus spaces
system ("CLS");
}
void pr_string()
{
cout << "my name is " << first_name << " " << surname << name << "\n";
}
-
September 21st, 2012, 08:47 PM
#9
Re: nter first and surname and then display the full name problem
I want to point out that name is still undefined. You declare it but never set it equal to anything, so it's contents are garbage. Since you had trouble with this, I think it might be a good exercise to play around with it. Get rid of all the global variables. Pass a string parameter to your print function. The best way to learn something new about a language is to work with it, but with small example programs. Get a feel for how that something works with a short, simple and easy to understand program, and then proceed to use it in a larger program. Hope this helps.
-
September 22nd, 2012, 09:17 AM
#10
Re: nter first and surname and then display the full name problem
Originally Posted by Alterah
I want to point out that name is still undefined. You declare it but never set it equal to anything, so it's contents are garbage. Since you had trouble with this, I think it might be a good exercise to play around with it. Get rid of all the global variables. Pass a string parameter to your print function. The best way to learn something new about a language is to work with it, but with small example programs. Get a feel for how that something works with a short, simple and easy to understand program, and then proceed to use it in a larger program. Hope this helps.
You're right that name is being output without ever being given a value, but wrong that it's contents are garbage. A string will be empty when it's declared.
-
September 22nd, 2012, 02:32 PM
#11
Re: nter first and surname and then display the full name problem
Originally Posted by GCDEF
You're right that name is being output without ever being given a value, but wrong that it's contents are garbage. A string will be empty when it's declared.
Whoops. Thanks for pointing that out.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|