Nov 17, 2011 at 6:22am Nov 17, 2011 at 6:22am UTC
I want to ask the user to input a number . After that ,the programme ask if he want to input a number again .i know how to make the loop. However, i don"t understand how to calaculate the sum of those number as i don"t know how many number the user will input
Another question is how can i convert lowercase to uppercase
Thx a lot
Nov 17, 2011 at 7:04am Nov 17, 2011 at 7:04am UTC
I had the exact same homework assignment like this, my solution was
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#include <iostream>
using namespace std;
int main()
{
double num[10], sum=0;
int i=0;
char option;
while (option!='n' ){
cin >> num[i];
cin >> option;
sum=sum+num[i];
i++;
}
cout << sum << endl;
system ("pause" );
return 0;
}
its not very good, but it works :)
enters a number into the array
if user enters "n" it will exit the loop
sums up the array
Last edited on Nov 17, 2011 at 7:07am Nov 17, 2011 at 7:07am UTC
Nov 17, 2011 at 8:35am Nov 17, 2011 at 8:35am UTC
@youichi
I think you can do it like that:
1 2 3 4
while (cin >> number) { //if input is a number,save it. if not, quit the loop.
sum += number;
++times;
}
You can use C function "toup(c)" to convert a char to uppercase.
Then you can write a loop to convert a string to uppercase.
Last edited on Nov 17, 2011 at 10:22am Nov 17, 2011 at 10:22am UTC