-
December 3rd, 2008, 09:35 AM
#1
how to get the string inputted by a user
Hi. I'm a college student taking up Computer Science right now we're just in repetition structure and i just want to know how to get the user inputted string..right now this is my program:
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
char name;
float ave1, ave2, ave3, genave;
for(int x=1; x<=10; x++)
{
cout<<"Enter Student's Name: ";
cin>>name;
cout<<"Enter 1st Term Average: ";
cin>>ave1;
cout<<"Enter 2nd Term Avergae: ";
cin>>ave2;
cout<<"Enter 3rd Term Average: \n";
cin>>ave3;
}
getch();
return 0;
}
i want the user to input his/her full name not just a nickname. If i use gets() the cursor will jump to Enter 1st Term Average and not in Enter Studen's Name.
-
December 3rd, 2008, 09:47 AM
#2
Re: how to get the string inputted by a user
-
December 3rd, 2008, 10:16 AM
#3
Re: how to get the string inputted by a user
when i use the code string instead of char it doesn't turn to blue. should i add another header file so the code string could be recognized?
by the way, i'm using microsoft visual c++ 6.0
-
December 3rd, 2008, 10:24 AM
#4
Re: how to get the string inputted by a user
Originally Posted by silent_assassin027
when i use the code string instead of char it doesn't turn to blue. should i add another header file so the code string could be recognized?
by the way, i'm using microsoft visual c++ 6.0
-
December 3rd, 2008, 11:54 AM
#5
Re: how to get the string inputted by a user
string will never be the same color as char (in a typical color-coded editor), because char is a built-in type, while string is a standard class.
Additionally, you should drop conio.h unless you actually need something there, as it's a nonstandard header. I realize you're using it for getch() here, but there are better ways to achieve the don't-close-until-I-tell-you-to behavior.
Last edited by Lindley; December 3rd, 2008 at 11:56 AM.
-
December 3rd, 2008, 01:44 PM
#6
Re: how to get the string inputted by a user
I realize you're using it for getch() here, but there are better ways to achieve the don't-close-until-I-tell-you-to behavior.
Mind sharing one of those better ways? I'm very interested in learning how to force a console window to remain open until the user closes it.
I'm not a student, I'm just learning on my own for Work.
-
December 3rd, 2008, 02:06 PM
#7
Re: how to get the string inputted by a user
Well, if you're using Visual Studio, just Start Without Debugging. The run will behave as if your program ends with "system("pause")", even if it doesn't. (If you Start Debugging, you can of course just place a breakpoint at the end of main.)
Of course, if you run the program from a preexisting console window it will have no reason to close when the program exits.
Otherwise, a simple getline() call (ensuring that there isn't a \n in the buffer beforehand of course) will also have the desired effect in a standard way----although you'll need to hit return specifically rather than any key.
-
December 4th, 2008, 01:59 AM
#8
Re: how to get the string inputted by a user
string will never be the same color as char (in a typical color-coded editor), because char is a built-in type, while string is a standard class.
Oh, I was wondering what "doesn't turn blue" means. LOL.
should i add another header file so the code string could be recognized?
Right. And the next time you have a problem, try to look up first in MSDN. You won't be able to do any serious programming until you start using it.
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
|