Getting a program to start over and get a new value? - c++

I am working with a program which requires a value to put in a variable and do some stuff on it. The problem is that I want the program to start over again and ask the user for a new value to process the new value again.
For example look at this code, which requires a number as a grade to rate it. When the processing was done I want the program to ask for a new grade ( next student for instance ).
#include <iostream.h>
int main (int argc, const char * argv[])
{
int n;
cout<< " Please Enter your grade : " ;
cin>>n;
switch (n/10) {
case 10: cout<< " A+ : Great! ";
case 9:
break;
case 8: cout<< " A : Very Good ";
break;
case 7: cout<< " B : Good " ;
break;
case 6:
case 5:
case 4:
case 3:
case 2:
case 1:
case 0: cout<< " Failed ";
break;
default:
break;
}
return 0;
}

What you need is a while loop
int main (int argc, const char * argv[])
{
int n;
while(1) {
cout<< " Please Enter your grade : " ;
cin>>n;
switch (n/10) {
case 10: cout<< " A+ : Great! ";
case 9:
case 8: cout<< " A : Very Good ";
break;
case 7: cout<< " B : Good " ;
break;
case 6:
case 5:
case 4:
case 3:
case 2:
case 1:
case 0: cout<< " Failed ";
break;
default:
break;
}
cout<<"do you wish to continue?(y/n)";
cin>>some_declared_variable;
if (some_declared_variable == 'n')
break; //hopefully this will break the infinite loop
}
return 0;
}

Related

Using while>>cin to keep asking user for input in C++

Code for a blackjack card counting program.
the issue is that it does not exit the while loop upon receiving no cin input from the user.
for example)
User would input x chars and then hit enter to exit the while loop.
#include<iostream>
using namespace std;
int main(){
int count = 0;
char currcard;
cout<<"Enter cards seen on table: "<<endl;
while (cin>>currcard)
{
switch (currcard)
{
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
count++;
break;
case '7':
case '8':
case '9':
break;
case 'A':
case 'J':
case 'Q':
case 'K':
count--;
break;
default:
cout<<"Invalid Entry";
break;
}
}
cout <<"Current count: "<< count << endl;
//user enter cards seen on table and if below 7 increment
//based on count the program returns if you should hit or quit
return 0;
}
Expecting program to exit when enter is hit by user
You can use cin.get() like this.
while (cin>>currcard)
{
// your logic
if (cin.get() == '\n') {
break;
}
}
In this way, your input is supposed to be something like 1 2 3 4 A J Q ending with Enter.
EDIT
As OP wants undetermined length of input, I suggest to switch the input itself from char to std::string.
This way access is gained to more intuitive and effective I\O operations:
#include <iostream> // std::cin, cout, endl
#include <string> // std::string: can omit this line
#include <cctype> // isspace(): can omit
int main(){
int count = 0;
std::string currcard{""};
std::cout << "Enter cards seen on table: "<< std::endl;
std::getline(std::cin, currcard);
for (char c : currcard) {
if (isspace(c))
continue;
switch (c) {
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
++count;
break;
case '7':
case '8':
case '9':
break;
case 'A':
case 'J':
case 'Q':
case 'K':
--count;
break;
default:
//std::cout << "Invalid Entry\n";
break;
}
}
std::cout <<"Current count: "<< count << std::endl;
//user enter cards seen on table and if below 7 increment
//based on count the program returns if you should hit or quit
return 0;
}
Notice I have added a check for white spaces, and removed the message for invalid entries: both simply get ignored. But if needed that line can be uncommented.
Old solution
You can use cin.getline() as suggested in the comments, in conjunction with a flag that triggers exit from the loop once three inputs are given:
#include <iostream>
int main(){
static int count = 0, flag = 0;
char currcard;
std::cout << "Enter cards seen on table: "<< std::endl;
while(std::cin.getline(&currcard, 3)){
switch (currcard)
{
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
++count;
break;
case '7':
case '8':
case '9':
break;
case 'A':
case 'J':
case 'Q':
case 'K':
--count;
break;
default:
std::cout << "Invalid Entry\n";
--flag;
break;
}
++flag;
if (flag == 3)
break;
}
std::cout <<"Current count: "<< count << std::endl;
//user enter cards seen on table and if below 7 increment
//based on count the program returns if you should hit or quit
return 0;
}
There is also a flag decrement for invalid entries.

Guessing Game with functions c++

I am writing a guessing game program using functions for each thing, I keep getting errors saying function isn't set so when I try to call it, it isn't working. I can't figure out what I am doing wrong.
I know I have arguments for the functions that aren't being used but I cant seem to figure out where or how I should include those in the function themself.
I am fairly new to programming/c++ so please no negative comments I am just trying to get as much help as I can.
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int getGuess(string prompt);
string getRank(int guessCount);
bool getPlayAgain(string prompt);
void playOneGame();
int main(){
srand(time(0));
int number = rand() % 100 + 1;
string prompt = getGuess();
do(
playOneGame();
)while(getPlayAgain())
return EXIT_SUCCESS;
}
int getGuess(string prompt){
int num;
int guessCount = 0;
prompt = cout << "Please enter a number between 1-100: ";
cin >> num;
if(num > 100){
cout << "Please enter a number between 1-100: " << endl;
}
if(num < 1){
cout << "Please enter a number between 1-100: " << endl;
}
if(num <= 100){
cout << "The number you guessed is: " << num << endl;
guessCount++;
}
}
string getRank(int guessCount){
switch(guessCount){
case 1:
case 2:
case 3: cout << "Lucky!" << endl;
break;
case 4:
case 5:
case 6: cout << "Awesome";
break;
case 7:
case 8:
case 9: cout << "Good";
break;
case 10:
case 11:
case 12: cout << "Meh";
break;
case 13:
case 14:
case 15: cout <<"Poor";
break;
default: cout << "Pathetic";
}
}
bool getPlayAgain(string prompt){
bool done = false;
int num1;
while(!done){
cout << "Enter 1 to play again or 2 to quit: ";
cin >> num1;
if(num1 == 2){
break;
}
else(
getGuess();
)
}
}
void playOneGame(){
getGuess();
getRank();
getPlayAgain();
}
No return statement in getguess() function but function signature is int return type.
Getguess() accepts prompt parameter as input but not used inside the function.

difference between a date and current date

I have a 14 day schedule and instead of memorizing it I wrote this. but I don't know how to get the current date in a format that will work for this program. C++11 is preferred but not required
#include <iostream>
#include <date.h>
using namespace std;
int main()
{
using namespace date;
using namespace std;
auto a = 2012_y/1/24;
auto b = current date; //I have not figured how to get current date
auto c = (sys_days{b} - sys_days{a}).count();
auto d = c%14
switch (d) {
case 1: cout << blank; //blank will be replaced with the schedule
break;
case 2: cout << blank;
break;
case 3: cout << blank;
break;
case 4: cout << blank;
break;
case 5: cout << blank;
break;
case 6: cout << blank;
break;
case 7: cout << blank;
break;
case 8: cout << blank;
break;
case 9: cout << blank;
break;
case 10: cout << blank;
break;
case 11: cout << blank;
break;
case 12: cout << blank;
break;
case 13: cout << blank;
break;
case 14: cout << blank;
break;
}
return 0;
}
It looks like you're using Howard Hinnant's date/time library.
You can get the current date in terms of a sys_days with:
auto b = floor<days>(chrono::system_clock::now());
This will be the current date in UTC. If you need it in a specific time zone, you'll need to use the time zone library (tz.h at the same location you found date.h).
If you're using C++17, floor can be found in <chrono>. Otherwise you can find it in "date.h".

Reading digits and converting to words

I'm new to c++ and I have to write a program that takes a user 4-digit number and convert it to words i.e. 7238 would be wrote as seven two three eight. Yet it writes every number as unknown. Any advice for a noob would be greatly appreciated.
#include iostream
using namespace std;
int main() {
char number;
cout << "Please enter a 4 digit number: ";
cin >> number;
switch(number){
case 1 :
cout<< "one";
break;
case 2 :
cout<< "two";
break;
case 3 :
cout<< "three";
break;
case 4 :
cout<< "four";
break;
case 5 :
cout<< "five";
break;
case 6 :
cout<< "six";
break;
case 7 :
cout<< "seven";
break;
case 8 :
cout<< "eight";
break;
case 9 :
cout<< "nine";
break;
case 0 :
cout<< "zero";
break;
default :
cout << "UNKNOWN.";
}
}
Sounds like homework but here are some tips. Change your number variable to type of int You can break the number out into individual variables with division and modulus. I would stuff those into an integer array.
int array[4];
arr[0] = (number / 1000) % 10; // Thousands
....... // You do the hundreds and tens
arr[3] = (number % 10); // Ones
Then use a loop around your switch statement where your counter is less than 4 (the length of the array). Make sure to increase your counter at the end of each loop. Oh, and it's #include <iostream>.
With to_string and range based for:
#include <iostream>
#include <string>
using namespace std;
int main()
{
int number;
cout << "Enter the number: ";
cin >> number;
string strnum = to_string(number);
for (auto c : strnum)
{
switch (c)
{
case '0': cout << "zero "; break;
case '1': cout << "one "; break;
case '2': cout << "two "; break;
case '3': cout << "three "; break;
case '4': cout << "four "; break;
case '5': cout << "five "; break;
case '6': cout << "six "; break;
case '7': cout << "seven "; break;
case '8': cout << "eight "; break;
case '9': cout << "nine "; break;
default: cout << "non-digit"; break;
}
}
return 0;
}
You need to put ascii values in your case statements. Currently you are comparing the ascii values for digits with numbers 0 - 9.
Values can be found here : http://www.asciitable.com/
Your variable is of type char. A char stores a character, usually ASCII encoded. If the user inputs a '1', for example, that would usually translate to an integer value of 49, not 1. Either read into an int or change your case labels to use character literals:
case '1':
cout << "one";
break;
You could then use a loop to read multiple digits.

How to use switch statements if you have four inputs

A class has four exams in one term. Input each exam score and assign the corresponding grades.
90 and above is A
80 to 89 is B
70 to 79 is C
60 to 69 is D
below 60 is F
below 0 is invalid
I can only code using 1 input not 4
#include<iostream>
using namespace std;
int main(){
int a;
cout<<"Input Examination Score: ";
cin>>a;
switch (a / 10){
case 0:
case 1:
case 2:
case 3:
case 4:
case 5: cout <<"Equivalent is F"<<endl;
break;
case 6: cout <<"Equivalent is D"<<endl;
break;
case 7: cout <<"Equivalent is C"<<endl;
break;
case 8: cout <<"Equivalent is B"<<endl;
break;
case 9:
case 10: cout <<"Equivalent is A"<<endl;
break;
default: cout <<"Invalid Score"<<endl;
break;
}
You can do this in different ways. You can either use a for-loop or a while/do-while loop.
for(int i = 0; i < 4; i++) // runs 4 times
{
//Do your thing
}
Or you can create a variable with the value of 4, use a while/do-while loop
and decrease it by 1 each semester until it is 0.
Use a loop to read the input multiple times, then calculate the average when printing the result:
#include<iostream>
using namespace std;
int main(){
int number_grades = 4;
int sum = 0;
for (int i=0; i<number_grades; ++i) {
int a;
cout<<"Input Examination Score: ";
cin>>a;
sum +=a;
}
switch ((sum/number_grades) / 10){
case 0:
case 1:
case 2:
case 3:
case 4:
case 5: cout <<"Equivalent is F"<<endl;
break;
case 6: cout <<"Equivalent is D"<<endl;
break;
case 7: cout <<"Equivalent is C"<<endl;
break;
case 8: cout <<"Equivalent is B"<<endl;
break;
case 9:
case 10: cout <<"Equivalent is A"<<endl;
break;
default: cout <<"Invalid Score"<<endl;
break;
}
return 0;
}