+ 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.
7 odpowiedzi
+ 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;
+ 19
You may post your code for inspection.
+ 17
Please provide sample input, expected output and actual output
+ 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.
+ 8
You can try it with other compilers.
+ 4
Actually, that works fine. This is the code. . .
+ 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;
}