Need your help people

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 :(?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
  #include <iostream>
 
using namespace 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;
 
    }else if (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.
Okay thank you man so much
Last edited on
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?
Last edited on
ne555 wrote:
1
2
3
bool valid(int n){
   return n>=100 and n<=999;
}


Please post the actual error messages rather than linking to a picture of them.
Last edited on
http://postimg.org/image/arot7lhin/ keep showing this whenever I enter a+ non 3 digit number it gets me back to 'COUT'
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
using namespace 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?
Last edited on
Topic archived. No new replies allowed.