Why the output of my code has 4745728 as the output? [closed] - c++

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;
}

Related

C++ need help, random switch statements [closed]

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.

Why the result differs when I press enter at the end of a sentence? [closed]

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 6 years ago.
Improve this question
Here is my first program
#include <iostream>
#include <string>
using namespace std;
int main()
{
int a;
string s;
double d;
while(cin >> a >> s >> d)
cout << a << s << d;
return 0;
}
When I input some simple data and press Enter , the result is shown immediately:
However, the code in another program behaves differently:
#include <iostream>
#include <string>
using namespace std;
struct Sales_data {
string bookNo;
unsigned units_sold = 0;
double price = 0.0;
void Print();
};
void Sales_data::Print(){//print every record of the Sales_data
cout << "The bookNo of the book is " << bookNo << endl;
cout << "The units_sold of the book is " << units_sold << endl;
cout << "The price of the book is " << price << endl;
}
int main()
{
Sales_data book;
while(cin >> book.bookNo >> book.units_sold >> book.price);
book.Print();
return 0;
}
When I run this code, input some data, and press Enter, it waits for me to input more data rather than show the result.
Could you explain this to me?
Remove the semicolon after the while loop. As it is, it forces the loop to have no body, which means it just cycles over the cin forever. Even better, use braces to delimit the body:
while(cin >> book.bookNo >> book.units_sold >> book.price) {
book.Print();
}

Function running twice in console [closed]

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();
}

Vector is not clearing [closed]

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
I have created a function that gets a series of guesses (a sequence of colors) from a user and puts them in a vector, and this function is called within a while loop in main().
Each time it is called by the while loop, the guess should be cleared before being refilled with inputs. However, within the second loop, entering a color I entered during the first loop activates my error message ("Invalid or repeated color entry..."), suggesting that the vector was not successfully cleared.
I've tried to clear it with a space, various strings, etc., but nothing seems to clear it. What am I missing?
Function:
void getGuess(vector<string> &currentGuessPegs, vector<string> &colorChoices, int maxPegSlots) {
string input; // stores input temporarily
// ---clear previous guess---
for (int i = 0; i < maxPegSlots; i++) {
currentGuessPegs[i] == "";
}
// ---prompt player for each peg guess and store in currentGuessPegs---
for (int i = 0; i < maxPegSlots; i++) {
cout << "Peg " << i+1 << ": ";
cin >> input;
while (find(currentGuessPegs.begin(), currentGuessPegs.end(), input) != currentGuessPegs.end() // Loops if color entry has already been used
|| find(colorChoices.begin(), colorChoices.end(), input) == colorChoices.end()) { // or is an invalid choice
cout << "Invalid or repeated color entry. See color choices and re-enter a color you have not used.\n";
cout << "Peg " << i + 1 << ": ";
cin >> input;
}
currentGuessPegs[i] = input;
}
}
And here is my call to the function from main():
// ---get and check guesses until maximum # of guesses is exceeded or solution is guessed---
while (guessCount < maximumGuesses && solutionGuessed == false) {
getGuess(currentGuess, colorOptions, numberOfPegs); // get the guess
solutionGuessed = checkGuess(currentGuess, solution, numberOfPegs, red, white); // check the guess; returns true if solution was guessed
cout << "r: " << red << " w: " << white << endl << endl;
guessCount++;
}
currentGuessPegs[i] == "";
// ^^
Whoops.

What is wrong with this C++ code [closed]

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 new to programming and I've decided to try and make a Calculator that can do stuff other than simple Arithmetic. I have not finished yet, I was just testing to see if it was working so far. As I ran it, and went through Arithmetic by pressing 1 it just stops. Can someone please tell me what Ive done wrong? Thank you.
#include <iostream>
using namespace std;
int main()
{
int frsnum
int secnum
int arithchoice;
int answer;
int x;
cout << "Welcome to the advanced calculator!" << endl;
cout << "What are you trying to calculate: Simple Arithmetic < 1 >" << endl;
cout << " Systems of Equations < 2 >" << endl;
cout << " Matrices < 3 >" << endl;
cin >> x;
if(x == 1)
{
cout << "Add <1>|Subtract <2>|Multiply <3>|Divide <4>";
cin << arithchoice;
}
if(arithchoice == 1)
{
cout << "Whats the first number: "
cin >> frsnum;
cout << "And the second number: "
cin >> secnum;
answer = frsnum + secnum;
cout << "That would be: " answer << endl
}
system("PAUSE");
return 0;
}
The arrows in this statement are incorrect.
cin << arithchoice;
should be replaced by this statement
cin>> arithchoice;
Update
The best way to remember which arrows to use with Cin and Cout is that with when inputing value you are pointing from outside to the computer.
Similarly for cout you throw values from the computer to outside world.
So now if you want to pass values from real world to computer you which arrow will you use >> cin
Similarly for giving results from computer to Real world(user) "<<"
----------------
| |
Real world | <--- computer |
|_______________|
The first thing I've noticed is that in the (x==1) if block, the arrows of the cin are the wrong way round.