C :: Use A Loop To Restart A Program
Feb 21, 2015
I've just started to learn C programming. In following program I need to use a loop, so at the end, it asks the user that if wants to try other numbers or not. If yes program restarts from beginning.
Code:
/*
A program who checks three positive integers to be 3 sides of a triangle and calculates the area and shows the triangle type.
*/
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int main(void)
[Code] .....
View 11 Replies
ADVERTISEMENT
May 11, 2013
I am having problems at the end of the program with the do-while loop to restart the program and the validation.
#include <iostream>
#include <string>
#include <iomanip>
#include <cstdlib>
#include <time.h>
using namespace std;
[code]....
View 3 Replies
View Related
Sep 30, 2013
I have a simple game loop.
while(true) {
//round initialization stuff
while(true) {
//capture input, make pieces move,
}
}
I am faced with the decision of what is the best way of restarting the game. The problem is, the condition for restarting a game is very, very deep inside the game's logic. So returning returning... is not an option, or at least, it ain't gonna be pretty.
So one of my considerations is to use a goto label, like so:
while(true) {
//round initialization stuff
while(true) {
//capture input, make pieces move,
} restart_round:
}
This seems to be the cleanest solution, since it allows me to fully reset the 'state' of the round, by first having all the destructors(pertaining to the round's objects) called, and then the constructors and other initialisation stuff.
Are there any subtleties that I am missing regarding this solution?
View 10 Replies
View Related
Aug 8, 2014
I just started learning c++ and got a problem now, i need a Y/N code, i case of yes the loop will restart and i case of no the program will end, Heres the code i got so far from trying to make my first steps.
#include <stdio.h>
void main() {
int a,b,c;
printf ("Zahl 1 Eingeben
");
scanf ("%i",&a);
[Code] ....
As u can see the other printf tasks are in german, but they are not important, the last one needs a Y/N "switch" y to restart the loop, and n to stop the program.
View 2 Replies
View Related
Feb 15, 2015
I have a question about my program.. I would like to make a program that if you put "ABC" on INT, the program will restart again... below are my coding...but it not works..if i put ABC on INT, the "Invalid Mark.. please re-insert mark :" will repeat, repeat and repeat infinitely...
Code:
#include <stdio.h>
void calcul() {int mark;
char * grade;
if (!(scanf("%d",&mark))){printf("
}
[code]....
View 7 Replies
View Related
Mar 5, 2013
I have to make a 8-puzzle and I got the algorithm i just dont know how to restart the program every time i type the move of the blank place in the 3x3 puzzle so i dont have to make 10000 lines to solve the puzzle
View 2 Replies
View Related
Jul 18, 2014
How can I take a variable value and put it in a DataBase to recover the value after a restart?
View 4 Replies
View Related
Oct 13, 2014
We are making a program--but every time we input a value for scanf, the following for loop does not work and the program quits without displaying the for loop's function. We are not getting any errors.
View 11 Replies
View Related
Apr 28, 2014
I need starting a do-while loop in my program that runs up to 10 time that asks the user to enter up to 10 scores, which include the slope and rating for the course, and calculates the handicap for each score.
Here's what I have so far:
Code:
{
//This program calculates a golfers handicap.
#include <iostream>
#include <string>
using namespace std;
[Code] ....
View 1 Replies
View Related
Feb 27, 2014
3. Write a program that reads a sequence of positive integers and prints out their sum, except that if the same number occurs several times consecutively, ignore all but the first. Assume an input of 0 marks the end of the input. For example, if the input is 3 8 5 5 4 9 1 1 1 1 8 0 then you should print 38 (i.e., ignore one of the 5's and three of the 1's).
View 5 Replies
View Related
Nov 20, 2014
I'm trying to write a program for a lab, I know how easy it is but cant see to crack it.
I need to make a program using while or for loop.
I must have the user type a number, for an input value an output value but then do it again several times but without it going on forever asking again and again.
View 4 Replies
View Related
Aug 29, 2014
How to make a loop so program never end, it end only by typing "EXIT"
View 1 Replies
View Related
Feb 20, 2013
I need a program in c++ that will enter a limit of the loop. using nested for loop with a limit of 3 loops only.
like this !
for (){
for (){
for (){
cout
}
}
}
Sample output:
Enter a number : 3 [enter]
1 2 3
4 5 6
7 8 9
2 3 4
5 6 7
8 9 10
3 4 5
6 7 8
9 10 11
View 2 Replies
View Related
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
Mar 4, 2014
I was asked by my teacher to create while loop for this, iv have completely for gotten how and all the tutorial that i have found show basic lopping.
#include <iostream>
#include <iomanip>
using namespace std;
void checkAnswers(char[], char[], int, int);
int main(int argc, const char * argv[]) {
int choice = 0;
const int num_question=20;
const int min_correct=15;
char answers[num_question] ={
[Code] ....
View 5 Replies
View Related
Jan 14, 2015
I'm trying to get this program to return total taxi cab fair an infinite number of times, however, when I use the while statement after for and try to run the program, it just keeps displaying the first total fair rapidly.
specifications:A cab company needs a program to calculate the cost of a cab ride.
The driver will enter the length of the trip in miles.
The miles entered must be greater than 0.
Create a function to calculate the charge. The function should receive one argument, the length of the trip, and should return the total fare for the ride.
The taxi charges a fixed charge of $2.50, plus an additional charge of $1.85 per mile.
Allow the user to calculate any number of trips.
At the end display the total of all the fares.
here is my code:
#include <iostream>
#include <iomanip>
using namespace std;
//function prototypes
double getLength ();
int main () {
[code]....
View 3 Replies
View Related
Feb 17, 2013
I'm writing a series of basic decipher programs and I have run into an issue where I get the correct answer when I start the loops at the iteration that contains the correct answer.
Code:
// generate key "words" with length of 3
for (int x = 0; x < 26; x++){
for (int y = 0; y < 26; y++){
for (int z = 0; z < 26; z++){
[Code] ....
This is the essence of the loop, I've attached the program in its entirety if necessary. Basically what happens is if I start the loops at x = 17, y = 7, z = 12, then I get the correct decipher shifts but if I start at 0,0,0 whenever it gets to that iteration (12,000 ish) the shifts are off by 2 or 3. "koq" should translate to "the" but im getting "dcz". Is this a simple bug in the or is something moving to fast for something else to keep up?
l3_ws.txt
main.cpp
View 1 Replies
View Related
Nov 15, 2013
I have an exam in C programming I'm a little stuck on loops..I have to make a program that multiplies the loop variable by 2 then takes away 1... easy, that was a for loop no problem
for (x=3; x<=65; x=x*2-1)
that should print out 3 5 9 17 33 65 however, my lecturer has given the task for it to reverse, but not to display the number 17 on the way back down using an if statement...however, this time it must be a do while loop... This is what I have so far.
Code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int x = 129;
}
[code]....
View 7 Replies
View Related
Feb 5, 2013
I have been working on a program to calculate the factorial of numbers. Part of my code is copied and modified from the FAQ about validating numbers in user input.
I have encountered a problem with the for loop that I am using near the end of my code. No matter what I do, it seems that my loop only does the multiplication of b = a*(a-1) and then prints. For example, inputting 5 will result in a print of 20, but the factorial is 120.
Code:
int main(void) {
char buf[BUFSIZ];
char *p;
long int a;
long int b;
long int i;
printf ("Enter a number to be factorialized: ");
[Code] ....
View 5 Replies
View Related
Jan 4, 2013
how to use while loop to control sudden termination program of win32 console application ?
View 1 Replies
View Related
Feb 21, 2014
My program asks me to write a C++ program that generates a random number between 1-100, and lets the user guess the number until he/she guesses correctly.
I have every thing done but my loop will not end. I know I have to "update" the loop to end it but I don't know what that means.
#include <iostream>
#include <iomanip>
#include <ctime>
using namespace std;
int main() {
srand(time(NULL)); //the function that generates random numbers
[Code] .....
View 2 Replies
View Related
Jul 10, 2013
Basically I have an tile class with the following variables: x, y, texture, and solid. I have created an array called tile MAP[map_tiles_x][map_tiles_y]. The object of the following snip-it of code is to loop through all of the tiles in the map placing their default values in each. For some reason this code does not work as planned, but in theory shouldn't it? I cannot see why it doesn't work. (BTW I do plan on open sourcing this map editor.
void init_map(){
for (int tile_y; tile_y < map_tiles_y; tile_y++){
for (int tile_x; tile_x < map_tiles_x; tile_x++){
MAP[tile_x][tile_y].x = tile_x * tile_size;
MAP[tile_x][tile_y].y = tile_y * tile_size;
MAP[tile_x][tile_y].solid = false;
std::cout << MAP[tile_x][tile_y].x << ", " << MAP[tile_x][tile_y].y;
} }
I should also add the effect of the code. Basically it only sets the default coordinates for the first object :/.
View 4 Replies
View Related
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
Sep 23, 2014
I am working on a program that calculates the average of a list of test scores entered, with the list ended with a negative score (which is not calculated), as long as a user enters a name.
#include<iostream>
#include<string>
using namespace std;
int main() {
string name;
double score = 0.0; //user input score
[code]....
I have gotten the while loop to function. The problem lies in the calculation in the average of the test scores, such as when I enter the scores 87 76 90 -1 , which is supposed to give an average of 84.3 but my result when outputted is 86. what the problem is and what I can do to fix this error?
View 1 Replies
View Related
Oct 21, 2014
I am trying to do a while loop that terminates the program when 99999 is entered but otherwise allows infinite integers to be entered. My problem is that when a negative number is entered it crashes and also when I enter a number it displays it twice.
#include <stdio.h>
#include <stdlib.h>
main() {
int number;
printf("Input an integer.
"); do {
scanf("%d",&number); /* read a single integer value in */
printf("%d
",number);
} while (number != 99999);
}
View 14 Replies
View Related
Mar 18, 2014
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.
testData8.dat:
Code:
12 9
13 756
View 3 Replies
View Related