Issue with simple while loop

So my current code works in Dev C++ but doesn't work in the SSH client (which I'm to understand is Unix). My 2nd to last line of 'cout' is to see what numbers are actually getting added. When in the SSH client val1 registers as 0 so val2 has no subtractions made to it. After that, all the other numbers come in just fine. Any help is appreciated!

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>
#include <cmath>
using namespace std;


int main()
{
        cout<<"Enter a number to find it's sum: ";
        int val1;
                
                while(cin>>val1){
        int val2 = val1/10000;
                cin>>val2;
        int val3 = val1/1000-(val2*10);
                cin>>val3;
        int val4 = val1/100-((val2*100)+(val3*10));
                cin>>val4;
        int val5 = val1/10-((val2*1000)+(val3*100)+(val4*10));
                cin>>val5;
        int val6 = val1-((val2*10000)+(val3*1000)+(val4*100)+(val5*10));
                cin>>val6;
        cout<<val2<<' '<<val3<<' '<<val4<<' '<<val5<<' '<<val6<<'\n';
        cout<<val1<<" sum is: "<<val2+val3+val4+val5+val6<<'\n';
        }
    return 0;
}


I use system("Pause"); when in Dev C++
You might want to move the std::cin on line 11 into the loow. What are you trying to check for on that line, anyways?

-Albatross
Last edited on
When I put the 'cin' into the loop it doesn't end, it'll repeat the 'cout's' over and over and over.

I change it to:

1
2
while(val1!='|'){
      cin>>val1;


Topic archived. No new replies allowed.