The goal is to create a five-in-one program for various math stuff. The individual blocks work. After selecting and using a block, the program should then send a prompt asking the user whether to go back to the menu or terminate the program, this also works.
However, the function which checks whether you've inputted "Y", "y", "N", or "n" does not work. Instead, it automatically returns you to the main menu even after inputting "N" or "n", which should terminate the program.
#include <iostream>
#include<cctype>
#include<cstdlib>
#include <ctime>
#include <bits/stdc++.h>
using namespace std;
bool Primecheck(int x){
// checks if prime is positive or nah
if (x <= 1)
return false;
// Check from 2 to n-1
for (int i = 2; i < x; i++)
if (x % i == 0)
return false;
return true;
}
void Repeatcheck(char repeat){
//checks whether you inputted anything other than Y, y, N, or n
while (repeat != 'Y' && repeat != 'y' && repeat != 'N' && repeat != 'n' ){
system("cls");
std::cout << "that's not an option, dumbass." << std::endl;
std::cout << "\n Return to main menu? Y/N" << std::endl;
cin >> repeat;
}
}
int main()
{
//Option selection
int OptSel;
//repeat selection
char repeat;
repeat = 'Y';
//Optsel 1
int prime, i, primed2=0, flag=0;
//Optsel 2
int j,rows;
//Optsel 3
int t1 = 0, t2 = 1, nextTerm = 0;
//Optsel 4
int factorialinput;
long factorial = 1.0;
do{
system("cls");
repeat = 'Y';
startoptsel:
std::cout << "\n----MATHS PROGRAM----" << std::endl;
std::cout << "\n wats poppin?" << std::endl;
std::cout << "1.) prime number checker" << std::endl;
std::cout << "2.) right triangle drawer" << std::endl;
std::cout << "3.) fibonacci" << std::endl;
std::cout << "4.) factorial" << std::endl;
std::cout << "5.) exit" << std::endl;
std::cin >> OptSel;
//check whether option seleccted is available.
if (!std::cin || OptSel <= 0 || OptSel > 5) {
system("cls");
std::cin.clear();
std::cin.ignore(1000, '\n');
std::cout << "\nThat's not an option, dumbass." << std::endl;
std::cout << "\nInput numbers from 1-5." << std::endl;
goto startoptsel;
}
if (OptSel == 1) {
system("cls");
primechecker:
std::cout << "\n----Prime number checker----" << std::endl;
std::cout << "Enter number: " << std::endl;
std::cin >> prime;
// auto filters everything as not prime below 1
primed2=prime/2;
for(i = 2; i <= primed2; i++){
if(prime % i == 0)
{
cout << "\n " << prime << " is not a prime."<<endl;
flag=1;
break;
}
}
if (flag==0)
cout << "\n " << prime << " is a prime."<<endl;
if (!cin) {
system("cls");
std::cin.clear();
std::cin.ignore(1000, '\n');
std::cout << "please input a number" << std::endl;
goto primechecker;
}
std::cout << "\n Return to main menu? Y/N" << std::endl;
cin>>repeat;
Repeatcheck(repeat);
}
if (OptSel == 2) {
system("cls");
triangledrawer:
cout << "\n----RIGHT TRIANGLE DRAWER----" << std::endl;
cout << "\nInput number of rows: ";
cin >> rows;
for(i=1;i<=rows;i++){
for(j=1;j<=i;j++)
cout<< ' ' << j;
cout<<endl;
}
if (!cin) {
system("cls");
std::cin.clear();
std::cin.ignore(1000, '\n');
std::cout << "please input a number" << std::endl;
goto triangledrawer;
}
std::cout << "\n Return to main menu? Y/N" << std::endl;
cin>>repeat;
Repeatcheck(repeat);
}
if (OptSel == 3){
system("cls");
fibonacciprinter:
cout << "\n----Fibonacci printer----";
cout << "\nEnter the number of terms: ";
cin >> prime;
cout << "Fibonacci Series: ";
for (int i = 1; i <= prime; ++i) {
// Prints the first two terms.
if(i == 1) {
cout << t1 << ", ";
continue;
}
if(i == 2) {
cout << t2 << ", ";
continue;
}
nextTerm = t1 + t2;
t1 = t2;
t2 = nextTerm;
cout << nextTerm << ", ";
}
if (!cin) {
system("cls");
std::cin.clear();
std::cin.ignore(1000, '\n');
std::cout << "please input a number" << std::endl;
goto fibonacciprinter;
}
std::cout << "\n Return to main menu? Y/N" << std::endl;
cin>>repeat;
Repeatcheck(repeat);
}
if (OptSel == 4){
system("cls");
cout << "Enter a positive integer: ";
cin >> factorialinput;
cout << "Factorial of ";
if (factorialinput < 0)
cout << "Error! Factorial of a negative number doesn't exist.";
else {
for(int i = 1; i <= factorialinput; ++i) {
cout << i << " ";
factorial *= i;
}
cout << "= " << factorial;
}
std::cout << "\n Return to main menu? Y/N" << std::endl;
cin>>repeat;
Repeatcheck(repeat);
repeat = repeat;
}
if (repeat == 'n' || repeat == 'N'){
system("cls");
std::cout << "\nJob done? JOB DONE! Aight wrap it up lesgo." << std::endl;
return 0;
}
}while (OptSel != 5);
system("cls");
std::cout << "\nJob done? JOB DONE! Aight wrap it up lesgo." << std::endl;
return 0;
}
Rewriting the program to instead run when repeat = Y or y and removing the specific instance of terminating the program when N or n is entered instead results in the program terminating automatically no matter what is inputted when the Repeatcheck function is called.
#include <iostream>
#include<cctype>
#include<cstdlib>
#include <ctime>
#include <bits/stdc++.h>
using namespace std;
bool Primecheck(int x){
...
}
void Repeatcheck(char repeat){
while (repeat != 'Y' && repeat != 'y' && repeat != 'N' && repeat != 'n' ){
system("cls");
std::cout << "that's not an option, dumbass." << std::endl;
std::cout << "\n Return to main menu? Y/N" << std::endl;
cin >> repeat;
}
}
int main()
{
...
do{
...
}while (OptSel != 5 && repeat == 'Y' || repeat == 'y');
system("cls");
std::cout << "\nJob done? JOB DONE! Aight wrap it up lesgo." << std::endl;
return 0;
}
Related
Really sorry if this is a dumb question. I know it must have a super easy solution but I've been staring at this for so long I can't see it. It doesn't help that I'm really new at this either.
Long story short for some reason entering an invalid input past the first time returns me back to my menu, and sometimes also asks me to enter weight immediately after instead of allowing me to enter a menu choice. It's just all around broken and I don't know why. Thanks.
#include <iostream>
#include <iomanip>
#include <limits>
using namespace std;
bool loopFlag = true;
bool loopFlagTwo = true;
int choice = 0;
int time = 0;
float weightPounds = 0;
float weight = 0;
const int BIKING = 8;
const int RUNNING = 10;
const int LIFTING = 3;
const float YOGA = 2.5;
int main()
{
cout << "Welcome to my Fitness Center" << endl;
do
{
cout << "\n\t____________________________________________________________" << endl;
cout << "\n\t\t\tMy Fitness Center" << endl;
cout << "\t\t\tActivity System" << endl;
cout << "\t____________________________________________________________" << endl;
cout << "\t\t\t Main Menu\n" << endl;
cout << "\t\t\t1) Stationary Bike" << endl;
cout << "\t\t\t2) Treadmill" << endl;
cout << "\t\t\t3) Weight Lifting" << endl;
cout << "\t\t\t4) Hatha Yoga" << endl;
cout << "\t\t\t5) End" << endl;
cout << "\t____________________________________________________________" << endl;
cout << "\n\nEnter the workout that you wish to track, or end to exit:" << endl;
do
{
cin >> choice;
if (cin.fail() || choice > 5 || choice < 1)
{
cout << "Invalid choice. Please choose from option 1 through 5." << endl;
cin.clear();
cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n');
}
else if (choice == 5)
{
return 0;
}
else
{
loopFlag = false;
}
}
while (loopFlag);
do
{
cout << "\nPlease enter your weight in pounds: " << endl;
cin >> weightPounds;
if (cin.fail() || weightPounds <= 0)
{
cout << "Invalid weight entry!" << endl;
cin.clear();
cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n');
}
else
{
loopFlag = false;
}
}
while (loopFlag);
weight = weightPounds / 2.2;
cout << "\nYour weight is: \n" << fixed << setprecision(1) << weight << " kilograms." << endl;
if (choice == 1)
{
do
{
cout << "For how many minutes did you do this activity? " << endl;
cin >> time;
if (cin.fail() || time <= 0)
{
cout << "Invalid time entry!" << endl;
cin.clear();
cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n');
}
else
{
loopFlag = false;
}
}
while (loopFlag);
}
}
while (choice != 5);
return 0;
}
You need to set loopFlag to true before every do...while() you have, or use another flag, because after the first do...while(), loopFlag is always false.
I have created a Prompt User to Loop to restart the program however when I type anything including "y" it just goes prompts me with "Press any key to continue . . . " and then closes. Here's my code:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
int Multiple1 = 0;
int Multiple2 = 0;
char again = 'Y';
while (again == 'y' || again == 'Y')
{
int Multiple1 = 0;
int Multiple2 = 0;
cout << "Input your first Multiple: " << endl;
cin >> Multiple1;
cout << "Your First Multiple is: " << Multiple1 << endl;
cout << "Input your second Multiple: " << endl;
cin >> Multiple2;
cout << "Your Second Multiple is: " << Multiple2 << endl;
for (int i = 1; i <= 100; i++)
{
if (i % Multiple1 == 0 && i % Multiple2 == 0)
cout << "FizzBuzz" << endl;
else
if (i % Multiple1 == 0)
cout << "Fizz" << endl;
else
if (i % Multiple2 == 0)
cout << "Buzz" << endl;
else cout << i << endl;
}
{
cout << "Restart? (y/n) ";
cin >> again;
}
system("pause");
return 0;
}
}
Thank you.
This is my first project and I am completely new to coding so I am not sure what to do.
Your return 0 is misplaced, it should be outside the while loop.
And you shouldn't put a bracket {} around your cin >> again.
{ // this bracket doesn't needed
cout << "Restart? (y/n) ";
cin >> again;
}
Just use bracket when it needed e.g. in for, if, function, etc. It will not make a compile error. But I think it makes you misread and think the while loop was closed.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
int Multiple1 = 0;
int Multiple2 = 0;
char again = 'Y';
while (again == 'y' || again == 'Y')
{
int Multiple1 = 0;
int Multiple2 = 0;
cout << "Input your first Multiple: " << endl;
cin >> Multiple1;
cout << "Your First Multiple is: " << Multiple1 << endl;
cout << "Input your second Multiple: " << endl;
cin >> Multiple2;
cout << "Your Second Multiple is: " << Multiple2 << endl;
for (int i = 1; i <= 100; i++)
{
if (i % Multiple1 == 0 && i % Multiple2 == 0)
cout << "FizzBuzz" << endl;
else
if (i % Multiple1 == 0)
cout << "Fizz" << endl;
else
if (i % Multiple2 == 0)
cout << "Buzz" << endl;
else cout << i << endl;
}
{ //This bracket doesn't need.
cout << "Restart? (y/n) ";
cin >> again;
continue;
} // This bracket doesn't need.
system("pause");
return 0;
}
}
my code compiles but it doesn't allow the user to guess the word correctly and when it displays the correct word, it is just a string of nonsense.
#include <iostream>
#include <fstream>
#include <string>
#include <ctime>
#include <cstdlib>
using namespace std;
int instructions();
void manual();
void file(char*);
int letterFill(char, char*, char*);
void Unknown(char*, char*);
const int MAX_LENGTH = 10;
void main()
{
bool done = false;
char word[MAX_LENGTH];
char unknown[MAX_LENGTH];
char letter;
char name[MAX_LENGTH];
int wrong_guesses = 0;
int MAX_TRIES;
char ans;
while (!done)
{
switch (instructions())
{
case 1:
{
manual();
break;
}
case 2:
{
file(word);
break;
}
}
cout << "WHAT IS THE NUMBER OF GUESSES ALLOWED?: " << endl;
cin >> MAX_TRIES;
Unknown(word, unknown);
cout << endl << endl << "HANGMAN";
cout << endl << endl << "Each letter is represented by a star." << endl;
cout << "You have " << MAX_TRIES << " tries left.";
cout << "ENTER GUESS WHEN READY: ";
cin >> letter;
while (letter != 'N' && letter != 'n')
{
while (wrong_guesses < MAX_TRIES)
{
cout << unknown << endl;
cout << "Guess a letter: " << flush;
cin >> letter;
if (letterFill(letter, word, unknown) == 0)
{
cout << endl << "You got it wrong! You lose a guess" << endl;
wrong_guesses++;
}
else
{
cout << endl << "Pfft, you got lucky" << endl;
}
cout << "Guesses Left: " << MAX_TRIES - wrong_guesses << endl;
if (strcmp(word, unknown) == 0)
{
cout << word << endl;
cout << "You got it!" << endl;
exit(0);
}
cout << "You've been hanged." << endl;
cout << "The word was : " << word << endl;
}
}
cout << "Try again? (y/n): " << flush;
cin >> ans;
if (ans == 'y' || ans == 'Y')
done = true;
else
done = false;
}
system("pause");
}
int instructions()
{
int select = 0;
cout << endl << "HANGMAN" << endl << endl;
cout << " PROGRAM MENU" << endl;
cout << " Select option 1 or 2" << endl << endl;
cout << " 1. INPUT WORD MANUALLY" << endl;
cout << " 2. PLAY AGAINST THE COMPUTER" << endl;
cout << " 3. EXIT PROGRAM BY INPUTING: N or n" << endl << endl;
cin >> select;
return select;
}
void manual()
{
string word;
cout << endl << "INPUT WORD: " << endl;
cin >> word;
return;
}
void file(char *roc)
{
ifstream fin("word.txt");
int x;
int count = 1;
int word;
int i = 0;
srand(time(0));
word = rand() % 20;
while (count < word)
{
fin >> x;
if (x == 0)
{
count++;
}
}
do
{
fin >> x;
roc[i++] = char(x);
} while (x);
return;
}
int letterFill(char guess, char *secretword, char *guessword)
{
int i;
int matches = 0;
for (i = 0; i<MAX_LENGTH; i++)
{
if (secretword[i] == 0)
{
break;
}
if (guess == guessword[i])
{
return 0;
}
if (guess == secretword[i])
{
guessword[i] = guess;
matches++;
}
}
return matches;
}
void Unknown(char *word, char *unknown)
{
int i;
int length = strlen(word);
for (i = 0; i<length; i++)
{
unknown[i] = '*';
}
unknown[i] = 0;
}
Again
my code compiles but it doesn't allow the user to guess the word correctly and when it displays the correct word, it is just a string of nonsense.
I'm solving some C++ problems from ebooks. I made this C++ program but it isn't working properly. I've 2 problems:
Even after applying the forumla (votePercentage = firstAnswer/totalVotes*100;) it isn't showing the output, but only 0.
The program should display the bar chart, how am I suppose to do that? Any hints, reference or solution will be appreciated.
Here is my code:
/*
* Write a program that provides the option of tallying up the
* results of a poll with 3 possible values.
* The first input to the program is the poll question;
* the next three inputs are the possible answers.
* The first answer is indicated by 1, the second by 2, the third by 3.
* The answers are tallied until a 0 is entered.
* The program should then show the results of the poll—try making
* a bar graph that shows the results properly scaled to fit on
* your screen no matter how many results were entered.
*/
#include <iostream>
#include <string>
void startPoll (void);
void showPoll (void);
void pollCheck (void);
std::string pollQuestion, answer1, answer2, answer3;
int pollChoice, firstAnswer, secondAnswer, thirdAnswer;
int main (void)
{
int totalVotes = 1;
float votePercentage;
startPoll();
showPoll();
for(;;totalVotes++)
{
if (pollChoice == 1)
{
firstAnswer = firstAnswer + 1;
}
else if (pollChoice == 2)
{
secondAnswer++;
}
else if (pollChoice == 3)
{
thirdAnswer++;
}
else
{
std::cout << "==============*======*======*==============\n"
<< " RESULT \n"
<< "==============*======*======*==============\n"
<< "Question: " << pollQuestion << "\n"
<< "Total Votes: " << totalVotes << "\n";
votePercentage = (firstAnswer/totalVotes)*100;
std::cout << answer1 << ": " << firstAnswer << " votes. | " << votePercentage << "\n";
votePercentage = secondAnswer/totalVotes*100;
std::cout << answer2 << ": " << secondAnswer << " votes. | " << votePercentage << "\n";
votePercentage = thirdAnswer/totalVotes*100;
std::cout << answer3 << ": " << thirdAnswer << " votes. | " << votePercentage << "\n";
return 0;
}
std::cout << "\nEnter your vote again\nOR\nuse 0 to show the results.\n";
std::cin >> pollChoice;
}
std::cout << "Error: Something went wrong!\n";
}
void startPoll (void)
{
std::cout << "Enter your poll question:\n";
getline (std::cin, pollQuestion, '\n');
std::cout << "Enter answer 1:\n";
getline (std::cin, answer1, '\n');
std::cout << "Enter answer 2:\n";
getline (std::cin, answer2, '\n');
std::cout << "Enter answer 3:\n";
getline (std::cin, answer3, '\n');
}
void showPoll (void)
{
std::cout << "==============|======|======|==============\n"
<< " POLL \n"
<< "==============|======|======|==============\n"
<< pollQuestion << "\n"
<< "1. " << answer1 << "\n"
<< "2. " << answer2 << "\n"
<< "3. " << answer3 << "\n\n"
<< "Enter 1,2 or 3:\n\n";
std::cin >> pollChoice;
pollCheck();
}
void pollCheck (void)
{
if (pollChoice != 1 && pollChoice != 2 && pollChoice != 3)
{
std::cout << "Wrong choice entered! Please try again.\n\n";
return showPoll();
}
}
You need to take care that integer/integer = integer. In your case, changing
(firstAnswer/totalVotes)*100
to
(1.0*firstAnswer/totalVotes)*100
or
(firstAnswer*100.0/totalVotes)
should work. They all give a floating point result.
Well, the solution for the Bar Chart could be the following:(Not written by me) I think thats very self explaining because its really basic
void line (int n, char c)
{
// this is the loop for n
for (int i = 0; i < n; i++)
cout << c << endl;
}
Here is my solution, you can see how I made the bars work by reading the comments.
#include <iostream>
using namespace std;
int main()
{
int a = 0;
int b = 0;
int c = 0;
cout << "What is your favorite animal? 1 Cat, ";
cout <<"2 Dog, 3 Fish, 0 Count votes" << endl;
//Choice counter
while (true)
{
int choice;
cout << "Choice: ";
cin >> choice;
if(choice == 1)
a++;
else if(choice == 2)
b++;
else if(choice == 3)
c++;
else if(choice == 0)
break;
else
continue;
}
cout << endl << " 1: " << a << endl;
cout << " 2: " << b << endl;
cout << " 3: " << c << endl;
cout << endl << "1\t" << "2\t" << "3\t" << endl;
//Finds the max voted option
int max = 0;
if(a > b && a > c)
max = a;
else if(b > c && b > a)
max = b;
else if(c > a && c > b)
max = c;
/* If the max voted option is bigger than 10, find by how much
we have to divide to scale the graph, also making 10 bar
units the max a bar can reach before scaling the others too */
int div =2;
if(max > 10)
{
do
{
max = max/div;
if(max < 10)
break;
div++;
}while(true);
}else
div = 1;
//Sets the final number for the bars
a=a/div;
b=b/div;
c=c/div;
if(a==0)
a++;
if(b==0)
b++;
if(c==0)
c++;
//Creates the bars
while(true)
{
if(a>0)
{
cout << "[]" << "\t";
a--;
}else
cout << " ";
if(b>0)
{
cout << "[]" << "\t";
b--;
}else
cout << " ";
if(c>0)
{
cout << "[]" << "\t";
c--;
}else
cout << " ";
cout << endl;
if(a==0 && b==0 && c==0)
break;
}
}
I am working with a c++ program, but I am stuck with annoying bug. The bug is that when i type the password, it counts backspace as a character so can I fix it? Here is the code.
#include <iostream>
#include <conio.h>
using namespace std;
int main() {
string password, username, lon, nu, np;
char c;
int StarNum = 0, humanproof;
cout << "Do you wanna create a user or login?";
cout << "\nLogin" << endl;
cout << "New" << endl;
cin >> lon;
if(lon=="login"){
goto login;
}
if(lon=="new"){
goto newa;
}
login:
cout << "Username: ";
cin >> username;
lol:
cout << "Password: ";
while (c != 13)
{
c = (char)getch();
if(c == 13) break;
StarNum++;
password += c;
cout << "*";
if (c == 127 || c == 8){
//go here to fix the problem
}
password = "";
goto lol;
}
}
if(username == "user" && password == "pass" || username == nu && password == np){
cout << "\nYou are logged in.";
goto options;
} else {
cout << "\nusername or password is wrong" << endl;
return 0;
}
return 0;
newa:
cout << "Username:";
cin >> nu;
cout << "password:";
cin >> np;
cout << "Type the number fourhoundred and twentie three too proof you are a human: ";
cin >> humanproof;
if(humanproof == 423){
cout << "The username is " << nu << endl;
for(int no = 0; no <= 100; no++){
cout << endl;
}
goto login;
return 0;
}
if(humanproof!=423){
cout << "wrong answer!";
return 0;
}
options:
int op;
cout << "\nwhat do you want to do?" << endl;
cout << "1. Calculator" << endl;
cout << "2. About"<< endl;
cout << "3. Just for fun" << endl;
cout << "4. Exit" << endl;
cin >> op;
if(op==1){
goto calculator;
}
if(op==2){
goto info;
}
if(op==3){
goto fun;
}
if(op==4){
return 0;
}
else{
cout << "you entered a invalid number. " << endl;
return 0;
}
calculator:
double n1, n2, sum;
int opa;
cout << "Choose a operation" << endl;
cout << "1. Addition/+" << endl;
cout << "2. Subscraction/-" << endl;
cout << "3. Multiplication/x" << endl;
cout << "4. Divsion/ /" << endl;
cin >> opa;
if(opa == 1){
cout << "enter number 1" << endl;
cin >> n1;
cout << "enter number 2" << endl;
cin >> n2;
sum = n1 + n2;
cout << "the sum is " << sum;
return 0;
}
if(opa == 2){
cout << "enter number 1" << endl;
cin >> n1;
cout << "enter number 2" << endl;
cin >> n2;
sum = n1 - n2;
cout << "the sum is " << sum;
return 0;
}
if(opa == 3){
cout << "enter number 1" << endl;
cin >> n1;
cout << "enter number 2" << endl;
cin >> n2;
sum = n1 * n2;
cout << "the sum is " << sum;
return 0;
}
if(opa == 4){
cout << "enter number 1" << endl;
cin >> n1;
cout << "enter number 2" << endl;
cin >> n2;
sum = n1 / n2;
cout << "the sum is " << sum;
return 0;
}
if(opa > 4){
cout << "You entered a invalid number";
goto calculator;
}
info:
cout << "Created by Bergur 2013";
return 0;
fun:
cout << "You want an eyepad(ipad)?";
}
Your code already checks:
while (c != 13)
And prevent newline from being handled, do similar to what you need with the backspace character, whose number is 8
To fix your issue, before:
StarNum++;
Add:
if (c == 8 && StarNum > 0) {
StarNum--;
if (password.size () > 0)
password.resize (password.size () - 1);
continue;
}
Please format your code. Also, try to provide a minimal code which reproduce the problem, not the whole code.
Unrelated to your problem
Try not to use goto.
Do not use ASCII values, but use the char literals instead, i.e. use '\n' instead of 13.
Also, you can simply add an iother condition inside your while:
while (c != '\n' && c != ' ')