Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
Basically I have finished programming a little times tables quiz for my little brother.I am very new to programming and I have no idea how to reset my program back to the start. Can anyone shed some light on this?
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main()
{
int sum ;
int question = 0;
int Uanswer ;
int score = 0;
int *score_pointer = &score;
//============================================================//
cout<<"= Hello Welcome to Your Times Tables ="<<endl;
cout<<"= Please Select A Table To Learn Below ="<<endl;
cout<<"========================================"<<endl;
cout<<"####################################"<<endl; /* THE MENU */
cout<<"####|1|2|3|4|5|6|7|8|9|10|11|12|####"<<endl;
cout<<"####################################"<<endl;
//============================================================//
cout<<">>:";
cin>> sum;
cout<<"You Selected "<< sum <<endl; /*User Input For Table */
cout<<"Time to Learn Brain Power Go!"<<endl;
//============================================================//
while (question <12){
question = question +1;
cout<< question;
cout<< "x";
cout<< sum ;
cout<< "=:";
cin>> Uanswer;
int Aanswer = (sum * question);
if (Aanswer == Uanswer){
cout<<"Correct: Well done Ben :)"<<endl;
*score_pointer = *score_pointer+1;
} else {
cout<<"Incorrect: Are you even trying? :("<<endl;
}
}
//====================================//
cout<<"Well done You scored " << score << " Out of 12"<<endl; /*Tally of total score */
//====================================//
return 0;
}
Use a do while loop to enclose your code and set the condition to what you want.
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main()
{
int flag=1;
do
{
//your code that needs to be repeated
//when you need to exit set flag=0
} while(flag);
}
Here is your program in a while loop. I would also recommend not using endl and instead using \n so you dont have to flush the stream so much.
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main()
{
bool done = false;
while (!done) {
int sum;
int question = 0;
int Uanswer;
int score = 0;
int *score_pointer = &score;
//============================================================//
cout << "= Hello Welcome to Your Times Tables =" << endl;
cout << "= Please Select A Table To Learn Below =" << endl;
cout << "========================================" << endl;
cout << "####################################" << endl; /* THE MENU */
cout << "####|1|2|3|4|5|6|7|8|9|10|11|12|####" << endl;
cout << "####################################" << endl;
//============================================================//
cout << ">>:";
cin >> sum;
cout << "You Selected " << sum << endl; /*User Input For Table */
cout << "Time to Learn Brain Power Go!" << endl;
//============================================================//
while (question < 12){
question = question + 1;
cout << question;
cout << "x";
cout << sum;
cout << "=:";
cin >> Uanswer;
int Aanswer = (sum * question);
if (Aanswer == Uanswer){
cout << "Correct: Well done Ben :)" << endl;
*score_pointer = *score_pointer + 1;
}
else {
cout << "Incorrect: Are you even trying? :(" << endl;
}
}
//====================================//
cout << "Well done You scored " << score << " Out of 12" << endl; /*Tally of total score */
//====================================//
cout << "\nWould you like to try again? (y/n)";
char answer;
cin >> answer;
tolower(answer);
if (answer == 'n') done = true;
}
return 0;
}
Related
I must write a program where the user can choose to practice with topic addition or topic multiplication that starts with a self-driven menu.
It must keep track of questions answered right, wrong and the number of questioned asked.
Which my current program is doing within each module(topic). Example Addition keeps track of the questions while the user is practicing Addition only and Multiplication does the same.
However, they are not being feedback to main, so they are not being added or displayed before the user can select another topic to practice or to exit the program.
Currently it is only to keeping track of the question (right /wrong/ total of questions) for each module (topic).
My goal is for the values to be passed to main and display the total number (right /wrong/ total of questions) before the user exits the program, but at the same time I must display the number of question in the Additional Topic and the Multiplication topic and provide a total.
Example Table of Addition, Multiplication and Totals ?
This is the code I have to start with. Can someone help me in how to code to return values of the (right /wrong/ total of questions) of the two topics and accomplish to display something like the table information.
******************************************************************************* /
#include <stdio.h> /* printf, scanf, puts, NULL */
#include <stdlib.h> /* srand, rand */
#include <time.h> /* time */
#include <string> // String managment funtions.
#include <iostream> // For input and output
#include <cmath> // For math functions.
#include <math.h>
#include <cstdlib>
using namespace std;
////////////////////////////////////////////////////////////////////////
// Implementing menu driven programs.
// Function Prototypes.
int menu();
void sums();
void products();
int main()
{
srand(time(0));
int option;
do {
option = menu();
switch (option) {
case 1: {
sums();
break;
}
case 2: {
products();
break;
}
default:
cout << "Program exit" << endl;
}
} while (option != 6);
return 0;
}
int menu()
{
cout << "Please select an option" << endl;
cout << "1) Practice with Addition " << endl;
cout << "2) Pratice with Multiplication " << endl;
cout << "3) Exit the program " << endl;
int option;
cin >> option;
return option;
}
void sums()
{
string keepgoing;
unsigned int quantity_total_questions = 0U;
unsigned int quantity_wrong_answers = 0U;
unsigned int quantity_correct_answers = 0U;
do {
const int minValue = 10;
const int maxValue = 99;
int y = (rand() % (maxValue - minValue + 1)) + minValue;
// cout<< " the random number is y "<< y << endl;
int x = (rand() % (maxValue - minValue + 1)) + minValue;
// cout<< " the random number is x "<< x << endl;
cout << "What is " << x << " + " << y << " =" << endl;
int answer;
cin >> answer;
if (answer == (x + y)) {
cout << "Great!! You are really smart!!" << endl;
++quantity_correct_answers;
++quantity_total_questions;
}
else {
cout << "Oh Sorry Try Again." << endl;
++quantity_wrong_answers;
++quantity_total_questions;
}
cout << "Right: " << quantity_correct_answers;
cout << " Wrong: " << quantity_wrong_answers;
cout << " Total Questions: " << quantity_total_questions << endl;
cout << "Do you want to play again? [enter y for yes or n for no]" << endl;
cin >> keepgoing;
} while (keepgoing == "y");
}
void products()
{
{
string keepgoing;
unsigned int quantity_total_questions = 0U;
unsigned int quantity_wrong_answers = 0U;
unsigned int quantity_correct_answers = 0U;
do {
const int minValueOne = 0;
const int maxValueOne = 9;
const int minValueTwo = 10;
const int maxValueTwo = 99;
int y = (rand() % (maxValueOne - minValueOne + 1)) + minValueOne;
// cout<< " the random number is y "<< y << endl;
int x = (rand() % (maxValueTwo - minValueTwo + 1)) + minValueTwo;
// cout<< " the random number is x "<< x << endl;
cout << " What is " << x << " x " << y << " =" << endl;
int answer;
cin >> answer;
if (answer == (x * y)) {
cout << "Great!! You are really smart!!" << endl;
++quantity_correct_answers;
++quantity_total_questions;
}
else {
cout << "Oh Sorry Try Again." << endl;
++quantity_wrong_answers;
++quantity_total_questions;
}
cout << "Right: " << quantity_correct_answers;
cout << " Wrong: " << quantity_wrong_answers;
cout << " Total Questions: " << quantity_total_questions << endl;
cout << "Do you want to play again? [enter y for yes or n for no]" << endl;
cin >> keepgoing;
} while (keepgoing == "y");
}
}
I would create a structure that contains the number of total answers and number of correct answers—the incorrect ones can be inferred—and then pass a reference to an instance of the structure to the respective sums() and products() functions.
Those functions can then populate the structure elements and when they return, your main function can read them out, knowing exactly how many questions were asked, how many were answered, or whatever other information you want to record and retrieve.
This question already has answers here:
Why is cout printing twice when I use getline?
(2 answers)
Closed 5 years ago.
When I run this and after I select my number as a player, Computer returns me two outputs (instead of one...). I have no idea why, could you please help me explain why that happens?
#include <iostream>
#include <string>
#include <stdlib.h>
#include <time.h>
using namespace std;
int random(int a, int b)
{
int num = a + rand() % (b + 1 - a);
return num;
}
int main()
{
srand(time(NULL));
int myNum;
cout << "Choose your number, human: ";
cin >> myNum;
int min = 1;
int max = 100;
int comp;
string player;
while(1) {
comp = random(min, max);
cout << "Computer: " << comp << endl; // why does this get called twice??
getline(cin, player);
if (player == "too high") {
max = comp - 1;
cout << "min: " << min << " max: " << max << endl;
} else if (player == "too low") {
min = comp + 1;
cout << "min: " << min << " max: " << max << endl;
} else if (player == "correct") {
cout << "Computer found the number..." << endl;
break;
}
}
}
It's because you are mixing input using >> and getline. getline reads to the next newline, >> does not. After you have entered your number, there is still a newline left behind, you have typed it, but hasn't yet been read. The first time you call getline that left behind newline gets read, and the program doesn't pause. Only on the second time that you call getline does your program pause and wait for you to type something.
Simple way to fix the problem is
int myNum;
cout << "Choose your number, human: ";
cin >> myNum;
// flush pending newline
string dummy;
getline(cin, dummy);
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
When I run my program it works the first time, the correct input is read and output is written to my output file but once I run it a second time it doesn't write anything the file is just blank even though it reads everything correctly. I can't seem to understand where its messing up or how and I really want to know. The two images are my first and second runs just that after the second run my output file is blank.
first run
second run
#include<iostream>
#include<iostream>
#include<cstdlib>
#include<time.h>
#include<fstream>
#include<string>
using namespace std;
int main()
{
int i=0;
int number; // The correct number
int guess=0; // The user's guess
int numGuesses=0; // The number of times the user has guessed
string lines;
string player;
ifstream ifile;
ofstream ofile;
//ofstream myfile;
//
string names[10];
int scores[10];
ifile.open("high_score.txt");
string first_last_name;
string temp;
int score;
int index=0;
string title;
bool topten=false;
cout <<"Welcome to the number guessing game. The top 10 best scores so far are: "<<endl;
while(!ifile.eof())
{
//getline(ifile,lines);
ifile >>first_last_name;
//cout << first_last_name;
ifile >> temp;
first_last_name.append(" ");
first_last_name.append(temp);
ifile >> score;
names[index]=first_last_name;
scores[index++]=score;
}
cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"<<endl;
for (int i=0; i < 10; i++)
{
cout << names[i] << " " << scores[i] <<endl;
}
// Seed the random generator.
srand(static_cast<int> (time(NULL)));
// Generate a random number between 1 and 100.
number = (rand() % 100) + 1;
cout << "Let's play the number guessing game! What is your name? " <<endl;
getline(cin,player);
cout << endl;
while(guess!=number)
{
cout << "guess the number the computer randomly picked between 1 - 100: ";
cin >> guess;
numGuesses++;
// Check if the user has guessed the correct number.
// If not, tell him if his guess is too low or too high
if(number > guess)
{
cout << "sorry, your guess is too low" << endl;
}
else if(number < guess)
{
cout << "sorry, your guess is too high" << endl;
}
else
{
cout << "You guessed right!!!!"<<endl;
cout << "You win!!!" << endl;
break;
}
}
cout << "It took you "<< numGuesses << " guesses "<< player<< endl;
////////////////////////////////////////////////////////////////////////
if (numGuesses<4)
{
cout << "Amazing! Or was it luck" << endl;
}
else if(numGuesses<6)
{
cout <<"That's a very good score..." <<endl;
}
else if (numGuesses<8)
{
cout << "That's pretty good but you can do better..." << endl;
}
else if ( numGuesses<10)
{
cout << "Not too shabby, but not too good either..."<< endl;
}
else
{
cout << "What a terrible score!..." << endl;
}
for(int i=0; i < 10; i++)
{
if(numGuesses <= scores[i])
{
for( int k=9;k>i;k--)
{
scores[k]=scores[k-1];
names[k]=names[k-1];
}
scores[i]=numGuesses;
names[i]=player;
topten=true;
break;
}
}
if(topten==true)
{
cout << "Hey, you made it to the top ten , Congratzzzzzzzz!!!!" <<endl;
}
ofile.open("high_score.txt");
for(int i=0;i<10;i++)
{
ofile <<"\t"<< names[i] << " " << scores[i] <<endl;
ofile << endl;
}
return 0;
}
You need to close the input file before you open the output file, since they are both referring to the same file.
The closing can be done explicitly by calling ifile.close() or implicitly by making ifile go out of scope before you open the output file.
The latter can be done like this:
{
ifstream ifile;
ifile.open("high_score.txt");
// do stuff with ifile
}
ofstream ofile;
ofile.open("high_score.txt");
// do stuff with ofile
Where is ifile.close() and ofile.close()
you should close else your stream will go into invalid state.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I get the error:
'_sleep': This function or variable has been superceded by newer
library or operating system functionality. Consider using Sleep
instead. See online help for details.
Can you guys and/or girls tell me what library is causing this error?
Code:
// A program to keep track of points and time and to give a random letter for the game scattergories
#include<iostream>
#include<ctime>
#include<string>
using std::cout;
using std::cin;
using std::string;
using std::getline;
void ltr() //gives a random letter
{
srand(time(NULL)); //gives a differant pattern every time
char letter;
letter = rand() % 27 + 64; //assigns a random letter in ascii code to a char (resulting in a random letter)
cout << "The letter is " << letter << "\n";
}
void timer()
{
cout << "You got 1.5 minutes to finish\n";
for (int i = 90; i > 0; i--)
{
if (i % 5 == 0)
cout << i << "\n";
_sleep(1000);
}
cout << "DING DONG!!! DING DONG!!! Time's up!!!\n";
}
void table()
{
int plr, ctr;
string lst[5][20]; //first dimantion: how many players. second dimantion: how many catagories, third dimantion(if added) will be the round
cin >> plr >> ctr; //parameters for later
cin.ignore(); //To avoid the "getline" reading the last input
for (int x = 0; x<plr; x++) //the player changes only after the previus player finishes
{
timer();
for (int i = 0; i<ctr; i++) //changing catagory
{
getline(cin, lst[x][i]);
}
system("cls");
cout << "Next player\n";
}
for (int x = 0; x<plr; x++) //this part (the whole "for" loop) is for confirming
{
cout << "Player number " << x + 1 << ": ";
for (int i = 0; i<ctr; i++)
{
cout << lst[x][i] << " ";
}
cout << "\n";
}
_sleep(5000);
}
int points() //points per round
{
int a, b, c, sum;
cout << "How many sections only you got?\n"; //worth 15 points
cin >> a;
cout << "How many words only you got?\n"; //worth 10 points
cin >> b;
cout << "How many words you and another person got?\n"; //worth 5 points
cin >> c;
sum = a * 15 + b * 10 + c * 5;
return sum; //Note: It doesn't matter how many sections there are.
}
int act()
{
int Points;
ltr();
table();
Points = points();
cout << "You have earned " << Points << " this round\n\n";
return Points;
}
int main()
{
int Points;
cout << "Starting in five seconds\n";
_sleep(5000);
Points = act();
for (;;) //inf loop
{
int ph;
cout << "Press 1 to continue or anything else to stop\n";
cin >> ph;
if (ph == 1)
{
Points += act();
}
else
{
break;
}
}
cout << "You have earned a total of " << Points << " great job!";
_sleep(5000); //time to read the last text
return 0;
}
/* To do list:
*Convert to arduino
*Make timer work in background of of table
*Check if words in the table (for differant players) are the same and give points accordingly
*Check if words are actual words (connect an online dictonary?)
*Make interface? (if possible and I have time to learn how)
*Think of what to do with Hardwear
*Comment rest of the code
*/
For using Sleep() function you need
#include <windows.h>
This question already has answers here:
How to start from beginning of the program
(6 answers)
Closed 6 years ago.
I just made code that calculates the multiplication tables, based on user input. The problem is that after I print the results, the program closes and I need to relaunch it to get different input and results.
I want to get the results and press a key to get a new value from the user: I don't want to be constantly closing and opening the the program. How can I do this?
This is my code:
#include <iostream>
using namespace std;
int main()
{
int a, range;
cout << "Introduce value: ";
cin >> a;
printf("\n");
cout << "Introduce range: ";
cin >> range;
printf("\n");
for (int i = 1; i <= range; ++i)
{
cout << a << " * " << i << " = " << a*i << endl;
}
printf("\n");
system("pause");
}
Add something like while(1)
#include <iostream>
using namespace std;
int main()
{
while(1){
int a, range;
cout << "Introduce value: ";
cin >> a;
printf("\n");
cout << "Introduce range: ";
cin >> range;
printf("\n");
for (int i = 1; i <= range; ++i)
{
cout << a << " * " << i << " = " << a*i << endl;
}
printf("\n");
system("pause");
}
}
Since the condition inside the while statement will always be true, your code here will loop forever!
If you want to press a key to determine if you want to continue with another value use the do while loop.
int main(void){
char c;
do{
//......your code
cout<<"Do you want to continue?"; // press 'y' if yes
cin>>c;
}while(c=='y');
return 0;
}
Press 'y' to continue or anything else to stop.
With this code you dont need system("Pause") at the end.