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);
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...
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.
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;
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;
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()
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(); } }
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
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();
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.
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.
I am unable to find why my code is going into infinite loop as below. This works perfectly fine if I keep entering just the integer values but if I enter a float number it ends up in an infinite loop
int main() { int x; while(1){ cin>>x; cout <<x; } return 0; }
I'm having trouble getting my loop to work correctly. If I iterate the for loop once it works as expected and displays proper output. When I try to iterate two times or more the program gets stuck in an infinite loop.
I am writing a program for grading a multiple choice test. The test data is pulled in from another file. My problem is that its only pulling in the first line of data and running an infinite loop without reading the next line of the data file.
I am getting and infinite loop for loop pointer variable current which points to head and is incremented by current->next in while loop. I use the pointer variable the same way in my display routine and it works. Here is listing of code.
#include "stdafx.h" #include<iostream> struct node{ int data; node *next;
I am working on a small simple program my program fills up a air plane with customers there is 2 levels 1 and 2 and it will put that person in the spot depending on being picked.
So far i made a boolean airplane with 10 spots I can enter 1 person into the airplane with no problem but then after he sits down i get a infinite loop? I assume i maybe have to use the continue statement I feel like there is something simple that im missing i want to add more people. here is what i have
I have an assignment where i have to make a palindrome program. As you can see i have already done the basic palindrome prog, however i am now stuck since the program goes into a infinite loop whenever a word with whitespaces is added.
I have also tried searching for methods online however they do not seem to work for me...
void CPali::check() { length = strlen(palin); for (int i = 0, p = length-1; p>i; i++, p--) { if (palin[i] == ' ')
I have a c program that I partially have working. The problem is basically writing a program that allows the user to input the amount of calories they plan to eat a meal and disperse the calories from top to bottom. My program produces the output in the example if I enter 1050 but the issue I noticed if the number of calories is just enough to cover the burgers I get negatives in the other variables.
For example, if I enter a total amount of calories of 1050, I can eat: Output: 2 burgers @ 770 calories (1050 - 770 = 280 calories remain) 1 bag of pretzles @ 170 calories (280 - 170 = 110 calories remain) 1 pear @ 80 calories (110 - 80 = 30 calories remain) 6 tsp. ketchup @ 30 calories If I input 1050 I get the above output but if I input a different integer such as 2000 this is my output 5 burgers @ 1925 calories 0 bag of pretzles @ 0 calories -1 apple @ -80 calories -35 tsp. ketchup @ -175 calories I can't give the full code since this assignment holds a lot of points and was up all night getting it work.
So I'll provide pseudocode
define all 4 variables burger 385, pretzel 170, pear 80, ketchup 5 print out text How many calories can you eat prompt user input Divide user input into burger How many burgers can bet eaten subtract calories eaten from original user input Divide calories left into pretzel How many bags can bet eaten subtract burger calories from pretzel calories Divide calories left after preztel into pear How many pretzels can be eatn subtract pretzels calories from pear calories Divide calories left over into ketchup how much ketchup can i use show on screen (int total)of burgers @ (int calorie total) calories show on screen (int total)bags of pretzels @ (int calorie total) calories show on screen (int total)pears @ (int calorie total) calories show on screen (int total)teaspoons of ketchup @ (int calorie total) calories
The problem I see is that subtracting the calories from the pear from the left over calories of the pretzel calories leads to a negative. If leftover calories minus 80(pear int) its less then 0 . The calculations from the pear onward to ketchup become incorrect resulting in negative output.
I've been making an RPG in C++ and I've used goto all throughout the program. After receiving an error and Googling how to fix it, I read that you shouldn't use goto. What can I use as a instead of goto, which isn't 'considered bad programming practice'?
Problem:Say I have a Base class called Car and it has 3 derived classes say Ford, Honda and Audi.
now the issue is all 3 derived classes have exactly same code but minor difference in calling member functions of their r respective engines( these engines have separate class) . example ford class calls code which is exactly same as other 2 classes but do call something like ford_engine-> start() ford_engine->clean() bla bla bla .
//ly Audi calls audi_engine->start() audi->clean();
now here the issue is it has redundant code in all 3 places with minute difference. Hoow I can have code at one place most probably in Base class and all derive classes uses same code.