C++ :: Keep Looping Until User Enters A Blank Line?
Aug 4, 2013
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?
View 1 Replies
ADVERTISEMENT
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?
View 2 Replies
View Related
Aug 18, 2014
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()?
View 1 Replies
View Related
Nov 21, 2014
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).
View 14 Replies
View Related
Oct 11, 2013
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;
class Stack {
[Code] .....
View 1 Replies
View Related
Jan 5, 2015
I have followed the tutorial here - [URL] .....
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...
"NumericControl.xaml"
<UserControl x:Class="[snip].UserControl.View.NumericControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
[Code] ....
View 14 Replies
View Related
Feb 14, 2014
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;
[Code] .....
View 2 Replies
View Related
Dec 5, 2013
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.
View 3 Replies
View Related
Jan 25, 2013
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.
View 2 Replies
View Related
Jan 28, 2015
Code:
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#include<string.h>
int main(void) {
int i;
char *str;
int len = 1;
[Code]...
View 3 Replies
View Related
Jul 4, 2013
I want to autogenerate employee id when user enters details of employee
Conditions:
User Selects Department which should be used as prefix.
A new Sequence of numbers must be generated for each department
Example:
DeptA0001
DeptA0002
Again DeptB new seuquence must be started
DeptB0001
DeptB0002...
View 3 Replies
View Related
Apr 17, 2015
Here's my code so far:
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.
View 13 Replies
View Related
May 1, 2014
How to do the problem below using loop?
Input numbers until the user types a 0, then output the product of the non 0 numbers: e
E.g., if the user types 2 4 6 0, the program should output 48
View 3 Replies
View Related
Sep 16, 2014
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];
[Code] ....
View 1 Replies
View Related
Dec 8, 2014
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];
[Code] ....
View 5 Replies
View Related
Jul 25, 2014
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.
do {
repeatflag = false;
cin >> UserInput;
UserInput = toupper(UserInput);
[Code] .....
View 2 Replies
View Related
Jun 10, 2013
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
[Code] ....
View 4 Replies
View Related
Mar 17, 2013
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 ~~~
[Code] ....
View 1 Replies
View Related
Nov 3, 2012
My program allow user to draw lines and the user should know the length of the line that he draw in centimeter (.cm)
I did use
SetMapMode(MM_LOMETRIC) function to convert the device units into logical units
and it works correctly but I don't know how the line length will appear to the user to let him draw in correct length
I think to make the length appear on the mouse when the user draw but I don't know how I calculate and display it.
View 2 Replies
View Related
Aug 15, 2013
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?
View 8 Replies
View Related
Aug 8, 2014
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)
You choose left.
Results:
Line 1. / hello
Line 2. / hello
View 8 Replies
View Related
Dec 8, 2013
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)
Code:
cout << "Customer Name: ";
cin >> name;
// ...
ofstream myfile("customer.txt", ios::app);
[Code] .....
View 3 Replies
View Related
Mar 7, 2014
How can a mulitline string be read line by line
ex: str = "PERIOD="week"
DAY="day"
TIME="time"";
View 2 Replies
View Related
Oct 30, 2014
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.
Code:
main.cpp:
#include "decompress.h"
int main(int argc, const char * argv[]) {
if(argc == 3) {
decompressor(argv[1], argv[2]);
[code].....
View 4 Replies
View Related
Mar 21, 2014
I am trying to draw a 2d grid array for a battleships game. I need the grid to have a border around each cell, how to do this. so far my code is:
#include<iostream>
using namespace std;
int main(){
int grid[5][5] = {{0}};
[Code] .....
View 4 Replies
View Related
Feb 17, 2013
//#include "stdafx.h"
#include<iostream>
#include<cmath>
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
View 2 Replies
View Related