C++ :: User Input - Infinite While Loop

Sep 5, 2014

I have a small loop that gets the user input, as well as acts as input validation to make sure he or she doesn't enter a value that is not within the specified range.

The user is supposed to enter a month number. E.g. if he or she chooses February, then enter 2, or December, 12.

It works correctly if they type a number that is not in the range, but goes into an infinite loop if, say, they type a string such as "month".

Code:
int main() {
// Variable Declaration(s)/Initialization(s)
int month=0;

// Get input
cout<<"Enter a month";
cin>>month;

[Code] ....

How could I go about revising this?

View 4 Replies


ADVERTISEMENT

C++ :: Loop Not Working User Input?

Jan 20, 2013

Some of my loop is not working. How messed up is this code.. Commented non working code

// ADIT.cpp : Defines the entry point for the console application.
#include "stdafx.h"
#include "windows.h"

[Code]....

View 3 Replies View Related

C++ :: Loop Depending On User Input

Jul 31, 2013

I'm trying to get this program to loop the number of times I get it to input. The program compiles alright, and it does loop when I tell it too, but how do I output the grades of the multiple students?

#include <fstream>
#include <iostream>
#include <iomanip>
#include <string>
#include <cstdlib>
int weighted1 = 0;
int weighted2 = 0;

[Code] .....

View 12 Replies View Related

C++ :: While Loop Keeps Looping Without Getting Input From User

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

C++ :: Indefinite User Input Loop?

Dec 13, 2014

In short, I'm trying to make a CLI for a project that can take multiple user inputs add I don't exactly know what I'm doing. I'm able to get cin to take one user input, but that's all I'm sure about.

View 8 Replies View Related

C/C++ :: While / For Loop User Input Sentence

Sep 15, 2014

I'm not sure what the program's purpose is?

1) Ask the user to enter a sentence. Until you reach the end of the sentence, test each character to count the number of spaces and letters.

2) For every space counted, print "SPACE", then print one "!" for every letter counted on an indented line underneath the "SPACE".

Your test sentence to use is: "The potash feldspars are important rock-forming minerals in plutonic, volcanic, and metamorphic rocks."

HINT: You do not need any strings. You need one while loop and two for loops. <cctype>

View 4 Replies View Related

C :: Unscramble User Input - Error In Loop

Sep 24, 2014

The purpose of this program is to unscramble the user's input. for example if the user entered 'ftooabll' the program would print 'football'. find the error that I am making....

Now, this only works for strings that are contained in the file wordlist. That being said, I would like this to repeat this search multiple times. Currently, the process is being repeated 7 times, but it only works on the first iteration. The code and sample input/output is below.

Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define NUMLOOP 6
void sort_string(char*);

[Code] ....

Sample input/output: (note: all data has been verified to be in the wordlist)

Enter string:
4132dcba
abcd1234
Enter string:
4132dcba
Enter string:
4132dcba
Enter string:
4132dcba
Enter string:
4132dcba
Enter string:
4132dcba
Enter string:
4132dcba

View 3 Replies View Related

C :: Fixing User Input Errors In A For Loop

Mar 6, 2015

So I have to write a step in my program that reads in up to 8 variables. Simple, but if the User submits an invalid value, I have to give an error message and ask him to re-enter the value until he enters a valid number. Here's my while loop that works perfectly:

Code:

while (true) {
printf("
Enter the mark given by Judge 1: ");
scanf("%f", &m1);
if ((m1 >= 0.0) && (m1 <= 10.0)) {
break;
}

[code]...

I'm not sure what to write in the else statement to make the programme understand that it has to repeat the exact same step but without adding 1 to index. Is this possible or do I have to suck it up and just use while loops instead? I'm very new to programming.

View 2 Replies View Related

Visual C++ :: Program That Take Input From A User And Calculate It In A Do While Loop

May 15, 2013

I'm trying to create a program that will take input from a user and calculate it in a do-while loop. The program does the calculation but the answer is wrong. The loop also doesn't work. The purpose of the program is to see how much an item will cost after a discount is taken off and tax is added.

#include <iostream>
#include <cmath>
using namespace std;
double original_cost;
double discount;
double tax;
double total;
char answer;

[Code]...

View 2 Replies View Related

Visual C++ :: Using Loop To Take User Input And Keep Running Sum Until 0 Entered

Feb 28, 2013

I am stuck on an exercise where i am supposed to use a loop to take user input and keep a running sum until the user enters a 0. the code i have so far is:

#include <iostream>
int main() {
using namespace std;
int num;
int total = 0;
int x;

[Code] ....

The full text of the error message is: error c2678:binary'>>':no operator found which takes a left-hand operand of type 'std::istream' . and one more thing i was wondering, is there a difference between c++ and visual c++?

View 4 Replies View Related

C :: How To Get Rid Of Infinite Loop

Mar 1, 2013

What I need to do to get rid of the infinite loop?

Code:
do {
printf("Enter the number of tests:");
scanf("%d", &test);
if (test< 0 || test> 4)
printf("Wrong number. Please try again!
");
}
while (test< 0 || test>4);

View 9 Replies View Related

C++ :: For Loop - Display Smallest And Largest Numbers From User Input

Jul 29, 2013

I'm suppose to write a program using (for loop) that asks the user to enter any amount of numbers, so that it can display the smallest and largest. My program successfully finds the largest, but it is always displaying 0 for the smallest, I think Im doing something wrong with the internalization but I dont know what to change it to.

This is what I have ....

#include <iostream>
using namespace std;
int main() {
int amount;
int count;
int number = 0;
int smallest = 0;
int largest = 0;
cout << "Enter total numbers to process: ";

[Code] ....

View 4 Replies View Related

C++ :: Stuck In Infinite Loop?

Feb 24, 2015

my code seems to enter an infinite loop should a user mistakenly enter a character other than a digit. The code is about selecting from a preset number of options, numbered 1- 4. If the user mistakenly enters a letter for example, instead of a number, the infinite loop kicks in...

View 1 Replies View Related

C/C++ :: Infinite Loop Of Asterisks Or Nothing At All?

Jan 27, 2015

So I have to create a program which will print, among other things, a rectangle made of asterisks. The rectangle has to follow this general format:

*****

* *

*****
but with the width and length of the rectangle being set by the user. I've tried every way I can think of to work this out, but I can't seem to get anything to work. The main errors I'm getting are either an infinite loop of asterisks filling my screen or nothing at all, depending on whether I use an && comparison or ||. A screenshot of my code is included below.

View 5 Replies View Related

C++ :: Stopping Infinite Loop?

Feb 20, 2013

The code below gives me the maximum odd number and the minimum even number of a list of numbers that the user type. However, for the loop to stop the user needs to type 1000. Is there a better way (without having to have a "stopping" number, just in case I need this number to be somewhere on the list) to get the same results?

Code:
#include <iostream>
#include <iomanip>
#include <cmath>
#include <string>
using namespace std;
int main() {
int x, maxi, mini;

[code]...

View 2 Replies View Related

C++ :: Program To Allow A User To Input A Number From Keyboard That Involves Loop Not Working Right?

Mar 30, 2014

I'm trying to write a C++ program that will allow a user to input a number from the keyboard. Then using a loop, that will perform 10 times, multiply the entered number by the loop counter. Print out the loop counter, the entered number and the product of the loop counter and the entered number. A one-time heading should be displayed before information is printed.

This kinda of what I have so far:

#include <iosteam>
using namespace std;
int main () {
Start
Declare: numScores, sum, score, avg, SENTINEL = 200
numScores = 0

[Code] ....

All the programs I have tried to make are not working?

View 4 Replies View Related

C++ :: Infinite Loop After Char Is Entered?

Nov 17, 2013

I have the program working but when I check if the input to make sure it is not a char it creates a infinite loop. I used an if statement to check for the issue and solve it but its not working. I even tried throwing an exception but I learned later they are not used for things like this.

Code:
#include <iostream>
#include <limits>
#include "contacts.h"
using namespace std;
int main()

[Code].....

View 10 Replies View Related

C/C++ :: Infinite For Loop - Program Just Closes

Feb 10, 2015

So I learned how to make a basic for loop and I decided to try my best to make an infinite one. Every time I run it, it doesn't say anything and just closes. Visual Studio doesn't say there's anything wrong with my code.

Here's the code

#include <iostream>
#include <conio.h>
using namespace std;
int main () {
int d = 9;
for(int k = 10; k < d; k = k +1) {
cout << "For loop value = " << k << endl;
getch();
} }

View 4 Replies View Related

C/C++ :: Infinite Loop While Reading From Pipe?

Oct 31, 2014

Why I have an infinite loop when reading from a pipe?

i = 0;
while(i < 10) {
test_value = read(pipe3[READING], &message, sizeof(struct MESSAGE));
printf("test_value is %d
", test_value);
//printf("Entering infinite loop

[code]....

View 10 Replies View Related

C++ :: Infinite Cout Loop In Else Statement?

Dec 12, 2014

I have more programming before this, but everything else works fine. My else loop has an infinite output. i also want it to output an error message if the value is not an integer and return to the input

cout << "Select number corresponding material for fencing:" << endl;
cout << "wood = 1" << endl;
cout << "stone = 2" << endl;
cout << "aluminium = 3" << endl;
cin >> material;
if (material == 2){
cost = 100 * perimeter;

[code]....

View 7 Replies View Related

C Sharp :: Stuck In Infinite Loop?

Sep 9, 2012

I currently have a hangman game in the making. Which is giving me debugging issues when I go to pick a letter, it will keep asking for a letter, if I place a break; within the loop it asks for a letter and says you've won. I know all I should need is a couple extra lines somewhere within the code.

/// Play game
public static string playGame() {
Words words = new Words();

[Code].....

View 2 Replies View Related

C++ :: Creating Multidimensional Array In Infinite Loop?

Aug 13, 2014

From the example given below, I would like to Generate a matrix who stores a generated array for each iteration. I have an understanding of inputting single elements to a matrix but how can I input an array to a matrix. let's say I would like to input and array of 4 elements that should be stored as a single row of the generated matrix and do this for an infinite while{true} loop.

#include <iostream>
#include <unistd.h>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <stdlib.h>
#include <stdio.h>
#include <vector>

void printArray(int arr[], int size) {

[Code] ....

View 7 Replies View Related

C++ :: Stop Infinite Loop By Pressing Enter

Apr 12, 2014

I wondering how to stop a infinite loop. The program clears a file then opens it and refreshes it over and over(the file is going to be modified by another program). I want it to start from the beginning anytime i press enter or escape, it doesn't really matter as long as you can restart it.

Here is the complete code:

#include <cstdlib>
#include <iostream>
#include <fstream>
#include <conio.h>
#include <windows.h>
using namespace std;
void x() {Sleep(1000);}

[Code] .....

View 2 Replies View Related

C++ :: Infinite Loop When Program Generates New Bracket

Feb 14, 2014

I am getting an infinite loop when my program generates a new bracket

#include <iostream>
#include <string>
#include <stdlib.h>
#include <numeric>
#include <fstream>
#include <vector>
using namespace std;
const int MAXTEAMS = 20;
const char* c;

[Code] ....

View 2 Replies View Related

C/C++ :: Infinite Loop If Non-numerical Character Entered

Jul 28, 2014

it will produce the answer above..when user enter a nonnumerical character it will force the user to reenter again

do {
cout<<"Min of the secret number : ";
cin>>min;
}while(cin>>min);
cout<<"Max of the secret number : ";
cin>>max;

this is my coding but it will auto jump out when i enter non-numeric character

View 7 Replies View Related

C/C++ :: Infinite Loop If Enter A Non-numberic Character

Jul 28, 2014

it will produce the answer above..when user enter a nonnumberic character it will force the user to reenter again

do
{
cout<<"Min of the secret number : ";
cin>>min;
}while(cin>>min);
cout<<"Max of the secret number : ";
cin>>max;

this is my coding but it will auto jump out when i enter non-numberic character

View 1 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved