0

CPP What is wrong with my code??

It was supposed to be a sum of all the numbers that I typed , but it appears with some enormous number at the end, help! #include <iostream> using namespace std; int main() { int num = 1; int number; int total = 0; int hajs; int roznica = 15; cout << "ksieciuniu..." << endl; while (num <= 5) { total += number; cout << "dej no jeszcze... ydwa złote\n"; cin >> number; if (number < 2) { cout << "kurua dej wincyj\n"; } num++; } cout << "mam już " << total << " złoty \n"; while (total <= 1

19th Dec 2017, 6:56 PM
Michał Mucha
4 Respostas
+ 1
You are adding number to the total before getting the input, which means number is uninitialized on the first iteration of your loop
19th Dec 2017, 9:36 PM
aklex
aklex - avatar
0
#include <iostream> using namespace std; int main() { int num = 1; int number; int total = 0; int hajs; int roznica = 15; cout << "ksieciuniu..." << endl; while (num <= 5) { total += number; cout << "dej no jeszcze... ydwa złote\n"; cin >> number; if (number < 2) { cout << "kurua dej wincyj\n"; } num++; } cout << "mam już " << total << " złoty \n"; while (total <= 15) { hajs = (total - roznica); cout << "dej no jeszcze " << hajs << endl; total += number; cin >> number; } cout << "ale byndzie chlansko :-D" << endl; return 0; }
19th Dec 2017, 6:57 PM
Michał Mucha
0
Sorry I think this was too long or sth
19th Dec 2017, 6:57 PM
Michał Mucha
0
thank youu
19th Dec 2017, 9:39 PM
Michał Mucha