C++ :: Keep Looping Until User Enters A Blank Line?
Aug 4, 2013
My goal is to create a loop that keeps taking user input over and over until the user doesn't enter anything into 'cin >>', leaves the line blank, and simply presses the ENTER key to move on, at which point the program is supposed to break out of the loop and continue on with the rest of program execution. Something like this:
Code:
do {
cout << "
Enter a name: ";
cin >> input1;
if (input1.empty())
[Code] ....
As you can see, I've already tried using the empty() function, but that didn't work, the program simply stays in the loop and doesn't break out, no matter how many times I press enter. It just keeps prompting me to enter a name. I've also tried using something like
Code: if (input1 == "")
but that doesnt work either. How do I break out of this loop?
So I've run into the following problem. My goal is to create a loop that keeps taking user input over and over until the user doesn't enter anything into 'cin >>', leaves the line blank, and simply presses the ENTER key to move on, at which point the program is supposed to break out of the loop and continue on with the rest of program execution. Something like this:
do { cout << " Enter a name: "; cin >> input1; if (input1.empty()) {
[Code] .....
As you can see, I've already tried using the empty() function, but that didn't work, the program simply stays in the loop and doesn't break out, no matter how many times I press enter. It just keeps prompting me to enter a name. I've also tried using something like
if (input1 == "")
but that doesn't work either. How do I break out of this loop?
I am writing a program for a class where the user enters their age, how much money they have, and their full name. I know that I have to use the getline method to get the user's full name, and I know that I have to use cin.ignore() before that code, but i'm not sure why I have to use cin.ignore()?
This is the problem :- Write a program that keeps reading integers until user enters -1, then it prints the maximum and the minimum among all numbers (-1 should be ignored).
I have my Reverse Polish calculator compiling and everything but for the assignment I need to handle a few exceptions that I can't seem to get. First off I'm trying to make the program exit if the user enters only "0" but since the input i'm using is string, I cant figure out how to code
"If the first node is 0 and the next node = NULL, return true"
Here is my code: #include<iomanip> #include<iostream> #include<stdlib.h> #include<stdio.h> #include<string> #include<sstream> using namespace std;
This is a tutorial that I have gone through completely with success, but now I am trying to create a different project using the same sort of setup.
When I add my usercontrol to the MainWindow it has none of the buttons/TextBlocks on which I have put on the UserControls - It is just a blank box. What could be causing this?
I have changed the name of the project to [snip] because it has a company name in it...
I am having trouble returning use input from iterations after the first series of input from the user. My readCourseArray function can only have one parameter and it is information from the structure Student. This information is gathered from a function which is not in the code below because it works fine. I have hard coded a few lines to try to figure out why I am not getting any input after the first pass of the readCourseArray function. I have tried to delete the tempArray in the function after each pass assuming that that could be the issue. But it was not. At least from what I have gathered. I have tried ignoring new line characters which I never really though was an issue but I tried anyways.
I guess what I am asking is: Is my problem coming from the function itself or from my assignment of the cArray to the return tempArray values?
Like I said, the first pass will print out all of the user input (if I put the code in) but all others will return bad values. I did have a do/while to print out the values but it caused the program to crash. I assume because it hit a bad value and needed to break.
#include <iostream> #include <string> using namespace std; struct Student { string firstName, lastName, aNumber; int numberCourses; double GPA;
struct stu_dat //outside main function { int rollno; char name[45], float average;
[Code] ....
No compilation problem.when executing prompt waits for inputting rollno, but, as soon as i enter a char string it keeps looping displaying the "want to enter more data?".i cant understand what is going on,as there is no compilation problem and runs good till i input the name.
If a user enters a string of boolean algebra it will ouput the table.
I have input parsing, cycling through the combinations, and outputing working. However once i parse the input I am not sure what to do with it. I have thought of having it write the parsed input to a new file as a function and then use that function, but that seems bad.
How to dynamically create the function, how to implement it.
BTW This is a console function, if that changes anything.
Code: #include <iostream> #include <iomanip> using namespace std; int main() { int dayNumber = 1;
[Code] ....
The code, when run, prompts the user to input an integer like this:
Code:
Day 1 Andy :>12 Bill :>7 Charlie :>15
Day 2 Andy :>5 Bill :>25 Charlie :>14 . . . etc.
Ok, so the code prompts the user to enter an integer for each of the 3 persons and then increments the "Day" and so on... When the user enters a negative value (-1, -2, etc.), the loop is broken and the program ends. Alright, everything is hunky-dory so far. 2 things which I can't figure out.
1. when the user doesn't enter anything and just hits return, the cursor drops a line and is still awaiting an input but doesn't show the prompt "Andy :>", for example. How can I get it to repeat the last prompt when the user doesn't enter anything? Like:
Code:
Day 1 Andy :> Andy :> Andy :>12 Bill :>25 Charlie :>15 . . etc.
2. When the user enters a letter or a special character, the program blows up. How can I correct this?
I've tried changing the data type for the variable used for the input, tried using getline, get, etc. With my current beginner knowledge, I'm missing something. So how can I get this to work?
The program should allow only integers to be entered, while allowing a negative number to trigger the loop to break or the program to end, and while re-prompting the last person if the user entered an invalid input.
int get_command_line (char * sa) { char * s; char * l = fgets(s, 300*5, stdin); int i = 0; int j; int n;
[Code] ....
The aim is to have the function return the number of arguments made after assigning each of the arguments to a c string stored in an array of five pointers. This is how I declare this:
int main(void) int n; char s0[300]; char s1[300]; char s2[300]; char s3[300];
I am trying to get my input from user in the same line with certain width between but my output is not in the same line. my third variable which is fruit[i] is shown one line below . How I can fix this ?
#include <iostream> using namespace std; int main () { int staff[3] , fruit[3];
My program gets user input and compares it against an array of characters (guessLetters[x]) to decide whether the do/while loop repeats. The whole character array contains underscores yet if i enter any letter it goes into the if statement and repeats the do/while loop.
I have been trying to make a small program that I will be using in a larger program that will make the user press space to see the next line of output.
Not exactly sure what I'm doing wrong, but I'm pretty sure that I need to put the space that the user would enter into an output file, and the read the input file
It runs, but it doesn't allow me press space before showing the next line.
I added a getline(cin, charVar) before, but I got a whole bunch of errors.
#include <iostream> #include <string> #include <cctype> #include <cstdlib> #include <fstream> using namespace std; // function for dialouge
I am having problems printing "->" on the beginning of each line, im trying to do it as a loop and ending it when the user types "q". also i would like to know how to ignore text from the user when the input begings with a specific character. heres what ive done so far, and not currrently working as expected.
Code: #include <stdio.h> int main (void) { char prompt; printf("~~~ FRACTION CALCULATOR ~~~
I want to be able to write a program where I can record user input but combine it with command line arguments. For example I do a simple getline(); and the user types test how to combine it so it could be like test /q and it gives a different output depending on what additional input was added. Can this be done with lots of different inputs?
How can I create in my C++ program so a user can paste a text and then choose a character on specific line and place?. The above thing isn't that important. I just want to place a character on a specific line and place.
I mean something like this:
Enter a character:
You choosed " / "
On which line do want the character?
You choosed "Line 1 and 2"
Where do you want the the to appear on the line? (left or right)
How to get this thing to work. All i need to do is ask user to input a name and then it brings out the line from the .txt file containing the information.
For example in my case I'm doing a member search function I'm required to ask user to input the name of the customer and then print out all the details (which consumes 1 text line in the .txt file)
Here is the code, This is the write to text file method (100% working)
I wrote a program to compress a file with gzip, which works fine. I also wrote a program to decompress the gzip file, which runs but outputs a blank file.
using namespace std; //class declaration class dayType{ public: int presday;
[Code] ....
So everything works perfectly, well not everything lol. Code compiles successfully without any errors but previous day does not calculate. In a sense that when it asks for number of previous days it returns a blank statement.
For example: Previous number day is: shows nothing