Solved

cin problems

Posted on 1998-12-02
3
251 Views
Last Modified: 2010-04-02
BASIS: i'm writing a program with AP classes, which means when i try to input a series of names from the user i have to make a while loop with cin.get(letter), pull in the whole name one character at a time, into the vector(basically an array).
PROBLEM: I found a problem that after prompting the user if they want to enter another name(assuming they type 'y' or yes) the program calls the function that inputs the name again.
only the second time(and every time after) cin doesnt prompt the user and instead ASSIGNS the variable to '\n' which is what i'm using as the escape key for the while loop that is pulling in the letters of the name.  so it escpaes the loop and messes up everything from there. and by the way, i have to use cin.get because names have spaces, and '\n' needs to be the escape key for the loop, for other various reasons.
any help would be GREATLY appreciated!
Thanks!
-T
0
Comment
Question by:t0astmAn
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 22

Expert Comment

by:nietod
ID: 1179146
Post your code so we can help you with it.
0
 
LVL 4

Accepted Solution

by:
pagladasu earned 70 total points
ID: 1179147
I think I can figure your problem with cin. Try something like this. It is a small code snippet which takes care of the enter key.

      char x,ans='y';
      while(ans=='y'){
            cout<<"Name:";
            while((x=cin.get())!='\n'){
                  //do whatever with x - stuff it into array
            }
            //don't forget '\0' at the end of array
            cout<<"Continue:";
            cin.get(ans);
            x=cin.get(); //get rid of another enter key
      }

Hope this will help. Thanks
pagladasu
0
 

Expert Comment

by:paulvz
ID: 1179148
You might also try the cin.getline function:

//This array should be the length of the name + 1
//Let's assume it should be 20 characters long.

char name[21];

while (/*repeat condition here*/)
{
  cin.getline(name, 20);
  //Process the data in name
}

The getline function will read 20 characters or until it reaches a delimiter (in this case \n which is default).  Also the delimiter is removed from the stream but not inserted into name.
0

Featured Post

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Getting IP address 8 123
Outlook 13 100
Need some help with mailto 16 43
what type of pointer is this pointer? 3 31
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question