Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

cin problems

Posted on 1998-12-02
3
Medium Priority
?
263 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 210 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

721 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