Solved

cin problems

Posted on 1998-12-02
3
257 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
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 viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

615 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