468,512 Members | 1,474 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,512 developers. It's quick & easy.

C++ Entering numerals of an integer horizontally

542 512MB
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?
Expand|Select|Wrap|Line Numbers
  1. int main ()
  2. {
  3. string s;
  4. char c;
  5. int sum=0,n=0,a[6]={0},num=0;
  6. cout<<"Enter a 6 digit integer \n";
  7. int i=0, fact=100000;
  8. while(i<6)
  9.    {cin>>n;
  10.    a[i]=n;
  11.    num+=a[i]*fact;
  12.    fact/=10;
  13.    i++;
  14.    if(i>5) break;      
  15.   }
  16. cout<<"The integer entered is: "<<num;
  17.  
  18. if(num%3==0)cout<<" and is divisible by 3 (= "<<num/3<<" )";
  19. else cout<<" and is not divisible by 3";
  20.  
  21. for(int i;i<6;i++)
  22.  sum+=a[i];
  23. cout<<"\nSum of numerals is: "<<sum; 
  24. if(sum%3==0)cout<<" and is divisible by 3\n";
  25. else cout<<" and is not divisible by 3\n";
  26. cout<<"\n";
  27. system("pause");
  28. return 0;
  29. }
Mar 3 '08 #1
7 4107
whodgson
542 512MB
i`ve modified the code to include this nexrInt() funct as follows:
Expand|Select|Wrap|Line Numbers
  1. int nextInt()
  2.     int a[6]={0}; 
  3.     char ch;
  4.     int i=0;
  5.     while (cin.get(ch))
  6.        { 
  7.         if (ch>= '0' && ch<='9')//next character is a digit
  8.         a[i]=ch;
  9.         i++;
  10.         if(i>=6) break;
  11.         }     
  12.         for(int i=0;i<6;i++)
  13.         cout<<a[i]<<" ";    
  14.  }  
/*
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.
Mar 3 '08 #2
weaknessforcats
9,207 Expert Mod 8TB
It's the cin.get() echoing the enter key you used to enter the character.

Maybe you could try getchar().
Mar 3 '08 #3
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.
Mar 3 '08 #4
weaknessforcats
9,207 Expert Mod 8TB
You will have to stop echoing the enter key. I don't remember if there is a way to do that.
Mar 3 '08 #5
oler1s
671 Expert 512MB
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...
Mar 3 '08 #6
mac11
256 100+
i`ve modified the code to include this nexrInt() funct as follows:
Expand|Select|Wrap|Line Numbers
  1. int nextInt()
  2.     int a[6]={0}; 
  3.     char ch;
  4.     int i=0;
  5.     while (cin.get(ch))
  6.        { 
  7.         if (ch>= '0' && ch<='9')//next character is a digit
  8.         a[i]=ch;
  9.         i++;
  10.         if(i>=6) break;
  11.         }     
  12.         for(int i=0;i<6;i++)
  13.         cout<<a[i]<<" ";    
  14.  }  
/*
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...
Mar 3 '08 #7
whodgson
542 512MB
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.
Expand|Select|Wrap|Line Numbers
  1. int intManip(int n)
  2. { int m=0,sum=0;
  3.  int a[6]= {0};  
  4.  for (int i=0;i<6;i++)       
  5.     { a[i]=n%10;
  6.       m=n/10;
  7.       n=m;
  8.       sum+=a[i];
  9.     }
  10.      return sum;
  11.  
  12. }              
/*
Enter a 6 digit integer 369258

Sum of digits: 33
*/
Thank you all
Mar 4 '08 #8

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

6 posts views Thread by Mutley | last post: by
13 posts views Thread by Christopher Benson-Manica | last post: by
4 posts views Thread by Patrick Blackman | last post: by
4 posts views Thread by Kun | last post: by
reply views Thread by NPC403 | last post: by
1 post views Thread by fmendoza | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.