okay when I run the program and enter non- 3 digit numbers it doesnt get me back to enter the new values and when it calculates the suma it prints all sum=i+1 not the final sum
So what is wrong with my code :(?
#include <iostream>
usingnamespace std;
int main()
{ int a,b,sum = 0;
cout << "Enter the first value!" << endl;
cin >> a;
cout << "Enter the second value!" << endl;
cin >> b;
if(a>100 || a>999){
cout << "The numbers you have entered are wrong,please enter ONLY a 3-digit number" << endl;
}elseif (b<100 || b>999){
cout << "The numbers you have entered are wrong,please enter ONLY a 3-digit number" << endl;
}
for(int i=a;i<=b;i++){
sum = sum + i;
cout << "The sum of your arithmetic progression is " << sum << endl;
}
return 0;
}
> it doesnt get me back to enter the new values
you never asked it to do it.
1 2 3 4 5 6 7 8 9 10 11 12 13
bool valid(int n){
return n>=100 and n<=999;
}
int main(){
//...
do{
cout << "Enter the first value!" << endl;
cin >> a;
cout << "Enter the second value!" << endl;
cin >> b;
}while( not valid(a) or not valid(b) );
> it prints all sum=i+1 not the final sum
your print statement is inside the loop
if you bother to indent properly you'll see it easily.
Hm I m thinking right now maybe we should program it so when we enter a NON 3 DIGIT NUMBER it says that our input is wrong and that we have to enter a 3 digit number?But that thing is already in 'if' loop but seems like it doesnt print it back?
#include <iostream>
usingnamespace std;
int main()
{
int a = 0, b = 0, sum = 0;
cout << "Enter the first value: " << endl;
cin >> a;
cout << "Enter the second value: " << endl;
cin >> b;
if( a<100 || a>999 ) {
cout << "The numbers you have entered are wrong,please enter ONLY a 3-digit number" << endl;
}
if( a>=100 && a<=999 )
{
for(int i=a; i<=b; i++) {
sum = sum + i;
}
cout << "The sum of your arithmetic progression is " << sum << endl;
}
return 0;
}
Okay fixed it but now when I enter non 3 digit numbers it prints
[/code]
"The sum of your arithmetic progression is " << sum
[code]
but it doesnt get me back to enter a new values so a user using this program should run this program again (I mean it's not so user-friendly)
I mean with what function should I get my program to get back on cin >> a; and cin >> b; part?