Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
Basically, whenever I run this program in console to test it, my Menu() function gets called and when I give input, it prints the whole Menu() function again. Please help me fix this.
p.s. This is incomplete.
#include <iostream>
#include <windows.h>
using namespace std;
int Menu();
int main()
{
float currMoney = 0;
float giveMoney = 0;
float coke = 8.50;
float fantaG = 9;
float fantaO = 9;
float creamS = 7;
Menu();
int Choice = Menu();
system("cls");
if(Choice == 1)
{
cout<< "Insert R"<< coke << endl;
int pay = 0;
cin>> pay;
float returnA = pay - coke;
if(returnA < 0)
{
returnA = -returnA;
cout<< "you still owe R"<< returnA << "0" << endl;
}
}
}
int Menu()
{
cout<< "[Drink machine v1.0]\n\n"<< endl;
cout<< "[1]Coke -- R8,50"<< endl;
cout<< "[2]Fanta grape -- R9,00" <<endl;
cout<< "[3]Fanta orange -- R9,00"<< endl;
cout<< "[4]Cream Soda -- R7,00"<< endl;
int Choice = 0;
cin>> Choice;
return Choice;
}
The reason it's being called twice is because you're calling it twice!
Menu(); // first time
int Choice = Menu(); // second time
In the first call you don't capture the return value - so all it does it show the menu, ask for input, and then discard the result.
In the second call you show the menu again, ask for input, and then this time you capture the result, and action on that result.
From the look of it you want to remove the first call to Menu()
You are calling it twice in the code
int main() {
Menu();
int Choice = Menu();
}
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I'm making a simple program to count user balance whenever there's a positive integer input between 1-5 from user. I only make the code for the first option, but something weird is happening from the output, here's my code :
#include <iostream>
using namespace std;
int balance = 100000;
int iNet = 59900;
int internet(int iNet, int code, int remain){
if(code == 1){
remain = balance - iNet;
cout << "Price = " << iNet << endl;
cout << "Remaining credit = " << remain <<endl;
}
}
int main(){
int code, remain;
cin >> code;
cout << internet(iNet, code, remain);
return 0;
}
But when i run the program, it shows like this :
user input
1
program output
Price = 59900
Remaining credit = 40100
4745728
I have no idea where's the 4745728 coming from.
If you don't want to return anything from a function, make the return type void:
void internet(int iNet, int code, int remain){
// ^
if(code == 1){
remain = balance - iNet;
cout << "Price = " << iNet << endl;
cout << "Remaining credit = " << remain <<endl;
}
}
Then compiler will not allow you to print a rubbish value anymore and you will have to fix your main:
int main(){
int code, remain;
cin >> code;
internet(iNet, code, remain);
//^ don't print anything, just call the function
return 0;
}
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
Why is this program returning random letters after output?
#include <iostream>
using namespace std;
int guessIt(int num) {
if(num == 27) {
cout << "You Won";
}
else {
cout << "You Lost";
}
}
int main() {
cout << guessIt(27);
}
There are some problems in your code:
#include <iostream>
using namespace std;
int guessIt(int num) { //when you make the function guessIt(int num) you say it returns an int
if(num == 27) {
cout << "You Won";
}
else {
cout << "You Lost";
}
} //code finished without returning an int
int main() {
cout << guessIt(27); //What happens here? guessIt didn't return anything, so cout prints something it got that was in the memory where guessIt was supposed to put it's return value
}
What you have to do is either add returns, like return 0; if you win and return 1; if you lost instead of cout<<"You won"; or cout<<"You Lost"; Then in main() add
if (guessIt(27)==0) {
//TODO: Win message here
}
else {
//TODO: Lose message here
}
You can also change guessIt's return value to void, and remove the cout<<guessIt(27) and just have guessIt(27). (void means it will return nothing.)
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I'm writing a code which is kind of like a fortune teller but I'm having some trouble with my switch statements. When executed the code prints out the same message and doesn't pick a random case like its supposed to! can someone please help me! thank you!
heres my code
#include<iostream>
#include<time.h>
#include<stdlib.h>
using namespace std;
void printGreeting(); // function prototype
int main()
{
int choice;
printGreeting();
cout << "Would you like to see your fortune?" << endl;
cout << "Press 1 to see your fortune or 2 if you don't!" << endl;
cin >> choice;
if (choice == 1)
{
cout << "Great! Your fortune is: ";
// Function to generate random number
void rand1();
srand(time(NULL));
int MAX_NUM;
MAX_NUM = 5;
int random = rand() % MAX_NUM;
cout << random << endl;
int selection;
selection = 5;
switch (selection)
{
case 1:
cout << "Change can hurt, but it leads a path to something better!";
break;
case 2:
cout << "If you have something good in your life don't let it go!";
break;
case 3:
cout << "You're in for a treat today.";
break;
case 4:
cout << "Land is always on the mind of a flying bird";
break;
case 5:
cout << "A dream you have will come true";
break;
}
return 0;
}
else if (choice == 2)
{
cout << "Okay goodbye!" << endl;
}
}
// Prints greeting message
void printGreeting() // function header
{
cout << "Hello! Welcome to your fortune teller!" << endl; // function body
}
Because selection = 5;
You want to choose selection with a random value between 1 - 5, right?
You switch by selection variable, which is explicitly set to 5 right before the switch itself. Consider switching by random variable.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 6 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
Whenever i try to run this game, it automatically returns 0.
The game is a text-based survival game that i coded on Code::Blocks. The compiler is MinGW. I am a semi-knowledgeable programmer. There is no error when compiled.
// This game automatically returns 0 and ends for some reason...
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
using namespace std;
int hunger;
int warmth;
int thirst;
int choice;
// Declares variables for hunger, warmth, thirst, and the users choice
int start()
{
cout<< "You are stuck in a forest, all alone"<< endl;
cout<< "You must maintain your hunger, thirst, and warmth." << endl;
int mainPage();
}
int hunt()
{
srand(time(0));
cout<< "You have chosen hunt!"<< endl;
if ((rand() % 2) == 2){
cout<< "You caught a deer!"<< endl;
hunger = hunger + 1;
}
else{
cout<< "You could not find anything..."<< endl;
}
int mainPage();
}
// The previous function is used for the hunting choice
int wood()
{
cout<< "You have chosen find firewood!"<< endl;
if ((rand() % 2) == 2){
cout<< "You found firewood!"<<endl;
warmth = warmth + 1;
}
else{
cout<< "You could not find any firewood"<< endl;
}
int mainPage();
}
// Wood choice
int water()
{
cout<< "You have chosen find water!"<< endl;
if ((rand() % 2) == 2){
cout<< "You have found a river!"<< endl;
thirst = thirst + 1;
}
else{
cout<< "You could not find anything..."<< endl;
}
int mainPage();
}
// Water choice
int mainPage()
{
warmth = warmth - 1;
hunger = hunger - 1;
thirst = thirst - 1;
// Subtracts one from each variable per turn
if (hunger == 0){
cout<< "You starved!"<< endl;
cout<< "Game over!"<< endl;
return 0;
}
if (thirst == 0){
cout<< "You became dehydrated!"<< endl;
cout<< "Game over!"<< endl;
return 0;
}
if (warmth == 0){
cout<< "You froze!"<< endl;
cout<< "Game over!"<< endl;
return 0;
}
// You die if any of the variables reach zero
cout<< "Your hunger is"<< hunger<< endl;
cout<< "Your warmth is"<< warmth<< endl;
cout<< "Your thirst is"<< thirst<< endl;
cout<< "What would you like to do?"<< endl;
cout<< "1 = hunt, 2 = find firewood, 3 = find water"<< endl;
cin>> choice;
if (choice = 1){
int hunt();
}
if (choice = 2){
int wood();
}
if (choice = 3){
int water();
}
// The main page that takes the users choice as input and also tells you the amount of each variable
}
int main()
{
hunger = 5;
thirst = 5;
warmth = 5;
int start();
}
// the main function
You have several issues with you code. First you are not calling your function correctly. When you have
int start();
in main() it doesn't call the start function but instead declares a function named start that returns an int and takes nothing. Knowing that you main function essential becomes
int main() {}
since you do nothing except set some variables that will never be used. A good compiler with a warnings turned on should have at least told you that you had unused variables.
You are going to have this same issue everywhere else you call a function as you declare a function instead of calling it.
Your second issue will come up after you fix your function calls. You will be using functions before you have defined them. A easy way to fix this is to declare the function prototype of all of your functions before you define/use any of them so the compiler knows that the function is going to exist.
A third issue is you use
srand(time(0));
in the function hunt(). This means that every time you call hunt you will reseed rand. instead of doing that you can put srand(time(0)); in main and then you will only seed rand once per execution of the program.
One last thing I see is none of your functions that are declared to return an int actually return anything. If you declare that a function has a return value then you need to return something from the function. If you do not want to return anything then you can make the return type void which means the function returns nothing.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I'm learning C++ so sorry for newbie question.
I'm doing exercises from S. Prata's Book. I'm currently on 6.4.
There is the code I've written:
#include <iostream>
using namespace std;
void showmenu();
void request();
const int strsize = 20;
const int templeSize = 5;
struct temple {
char name[strsize];
char job[strsize];
char psd[strsize];
int preference;
};
int main(){
temple members[templeSize] = {
{"Alan", "spy", "Kret", 0},
{"Bruce", "engi", "Mech", 2},
{"Zac", "engi", "Robot", 0},
{"Kevin", "teacher", "Kid", 1},
{"Maverick", "spy", "Shadow", 2}
};
char choice;
showmenu();
request();
cin >> choice;
while (choice != 'q'){
switch(choice){
case 'a' : for(int i; i< templeSize; i++)
cout << members[i].name << endl;
break;
case 'b' : for(int i; i< templeSize; i++)
cout << members[i].job << endl;
break;
case 'c' : for(int i; i< templeSize; i++)
cout << members[i].psd << endl;
break;
case 'd' : for(int i; i < templeSize;i++){
switch(members[i].preference){
case 0: cout << members[i].name; break;
case 1: cout << members[i].job; break;
case 2: cout << members[i].psd; break;
}
}
default : request();
}
showmenu();
cin >> choice;
}
cout << "\nBye!\n";
return 0;
}
void request(){
cout << "Choose one option:\n";
}
void showmenu(){
cout << "a. names b. jobs\n"
"c. psds d. preferences\n"
"q. Quit\n";
}
I have no ide what is wrong with that. Code is compiling (I'm using code::blocks), but only for cases 'a' and 'b'. When I input 'c' or 'd' it just showing menu again. Same if I choose a/b more than once.
I've found other solution via google, but I realy want to know what is wrong with my code.
i is not initialized in any of your case statement for loops
You are calling showmenu() outside of your switch statement. So no matter what the input you will leave the switch and call the function.
switch(choice){
...
}
showmenu();
...