+ 5

Can Sololearn's compiler be incorrect?

I've created an simple arithmetic program and it keeps on giving the wrong output even though nothing is wrong with the code.

29th Mar 2017, 1:10 AM
Rajae
Rajae - avatar
7 Answers
+ 7
I can see you're not initializing "totalage", for one. If you don't initialize a variable, you're just going to get junk memory. You want: int totalage = 0;
29th Mar 2017, 2:00 AM
Squidy
Squidy - avatar
+ 19
You may post your code for inspection.
29th Mar 2017, 1:18 AM
Hatsy Rei
Hatsy Rei - avatar
+ 17
Please provide sample input, expected output and actual output
29th Mar 2017, 1:55 AM
Jafca
Jafca - avatar
+ 8
If you are talking about your "basic calculator (0-9)" code, you are using the "if" incorrectly. Replace if (num1, num2, num3 >= 10) with if (num1 >=10 && num2 >= 10 && num3 >= 10) and it will work.
29th Mar 2017, 1:25 AM
Krishna Teja Yeluripati
Krishna Teja Yeluripati - avatar
+ 8
You can try it with other compilers.
29th Mar 2017, 1:54 AM
Pravin Pandey
Pravin Pandey - avatar
+ 4
Actually, that works fine. This is the code. . .
29th Mar 2017, 1:29 AM
Rajae
Rajae - avatar
+ 4
#include <iostream> using namespace std; int main() { int age; int totalage; int numofpers = 0; cout << "Enter first person\'s age: " << endl; cin >> age; while (age != -1) { totalage += age; numofpers++; cout << "Enter next person's name or enter -1 to cancel." << endl; cin >> age; } cout << "Your total age is " << totalage << endl; cout << "Your average age is " << totalage/numofpers << endl; }
29th Mar 2017, 1:29 AM
Rajae
Rajae - avatar