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
4 Answers
+ 1
You are adding number to the total before getting the input, which means number is uninitialized on the first iteration of your loop
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;
}
0
Sorry I think this was too long or sth
0
thank youu