Solved

cin problems

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
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 how to clear a vector as well as how to detect empty vectors in C++.

747 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now