CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Dec 2008
    Posts
    8

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

  2. #2
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: how to get the string inputted by a user

    Replace
    Code:
    char name;
    with
    Code:
    string name;
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  3. #3
    Join Date
    Dec 2008
    Posts
    8

    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

  4. #4
    Join Date
    May 2006
    Location
    Norway
    Posts
    1,709

    Re: how to get the string inputted by a user

    Quote Originally Posted by silent_assassin027 View Post
    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
    Code:
    #include <string>

  5. #5
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    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.

  6. #6
    Join Date
    Dec 2008
    Posts
    144

    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.

  7. #7
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    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.

  8. #8
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    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.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured