C++ Entering numerals of an integer horizontally

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • whodgson
    Contributor
    • Jan 2007
    • 542

    C++ Entering numerals of an integer horizontally

    The following code does everthing required except that because each numeral of the integer has to be entered individually the numbers appear in a vertical column. Question: is there a way of entering the numbers so that they appear on the screen in one row each having been entered individually?
    Code:
    int main ()
    {
    string s;
    char c;
    int sum=0,n=0,a[6]={0},num=0;
    cout<<"Enter a 6 digit integer \n";
    int i=0, fact=100000;
    while(i<6)
       {cin>>n;
       a[i]=n;
       num+=a[i]*fact;
       fact/=10;
       i++;
       if(i>5) break;      
      }
    cout<<"The integer entered is: "<<num;
    
    if(num%3==0)cout<<" and is divisible by 3 (= "<<num/3<<" )";
    else cout<<" and is not divisible by 3";
    
    for(int i;i<6;i++)
     sum+=a[i];
    cout<<"\nSum of numerals is: "<<sum; 
    if(sum%3==0)cout<<" and is divisible by 3\n";
    else cout<<" and is not divisible by 3\n";
    cout<<"\n";
    system("pause");
    return 0;
    }
  • whodgson
    Contributor
    • Jan 2007
    • 542

    #2
    i`ve modified the code to include this nexrInt() funct as follows:
    Code:
    int nextInt()
    { 
        int a[6]={0}; 
        char ch;
        int i=0;
        while (cin.get(ch))
           { 
            if (ch>= '0' && ch<='9')//next character is a digit
            a[i]=ch;
            i++;
            if(i>=6) break;
            }     
            for(int i=0;i<6;i++)
            cout<<a[i]<<" ";    
     }
    /*
    Enter a 6 digit integer 123698
    49 50 51 54 57 56 //this should be same as input but..........
    */
    I'm obviously doing something wrong and illegal which is probably the while arguement.

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      It's the cin.get() echoing the enter key you used to enter the character.

      Maybe you could try getchar().

      Comment

      • Simonius
        New Member
        • Feb 2008
        • 47

        #4
        That'll still have to same problem.
        A possible solution might be a variant of gotoxy but that'll be a bit cumbersome and you'll have to take into account some constraints.

        Comment

        • weaknessforcats
          Recognized Expert Expert
          • Mar 2007
          • 9214

          #5
          You will have to stop echoing the enter key. I don't remember if there is a way to do that.

          Comment

          • oler1s
            Recognized Expert Contributor
            • Aug 2007
            • 671

            #6
            You would have to play around with the console functions (assuming Windows) and change the way the console worked. And add in some additional code yourself. Have fun...

            Comment

            • mac11
              Contributor
              • Apr 2007
              • 256

              #7
              Originally posted by whodgson
              i`ve modified the code to include this nexrInt() funct as follows:
              Code:
              int nextInt()
              { 
                  int a[6]={0}; 
                  char ch;
                  int i=0;
                  while (cin.get(ch))
                     { 
                      if (ch>= '0' && ch<='9')//next character is a digit
                      a[i]=ch;
                      i++;
                      if(i>=6) break;
                      }     
                      for(int i=0;i<6;i++)
                      cout<<a[i]<<" ";    
               }
              /*
              Enter a 6 digit integer 123698
              49 50 51 54 57 56 //this should be same as input but..........
              */
              I'm obviously doing something wrong and illegal which is probably the while arguement.
              I don't know about the echoing thing, but your cramming a char into an int, thats not gonna work the way you want it to.
              Say somebody puts in 9 - Ascii '9' isn't the same as int( 9 ) so when you do a[i] = ch your not storing 9, your storing the character '9', which has int value of 57.

              Or maybe I'm confused...
              Last edited by mac11; Mar 3 '08, 08:37 PM. Reason: typo!

              Comment

              • whodgson
                Contributor
                • Jan 2007
                • 542

                #8
                Thank you for your comments--wish i had your insights.
                I couldn`t see the wood for the trees.
                This is what i finally came up with which works fine.
                Code:
                int intManip(int n)
                { int m=0,sum=0;
                 int a[6]= {0};  
                 for (int i=0;i<6;i++)       
                    { a[i]=n%10;
                      m=n/10;
                      n=m;
                      sum+=a[i];
                    }
                     return sum;
                     
                }
                /*
                Enter a 6 digit integer 369258

                Sum of digits: 33
                */
                Thank you all

                Comment

                Working...