473,405 Members | 2,415 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

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 4965
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,208 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,208 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

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

Similar topics

6
by: Mutley | last post by:
I am finding an issue with the current DropDownList and ListBox ASP.NET Web Controls. I had a system set up for Arabic and a string that I put into a drop down had a number. The letters were being...
13
by: Christopher Benson-Manica | last post by:
Inspired by a thread on clc++, I decided to try it out... #include <stdio.h> #include <stdlib.h> int main( int argc, char *argv ) { int i; int result=0; int this;
4
by: Patrick Blackman | last post by:
How do you write regular numbers eg 1 23 475896 in roman numerals inC#
4
by: Kun | last post by:
I have the following if statement that checks if a form is empty: if form.has_key("date") and form.value != "": date=form.value else: print "ERROR: No date entered!" raise Exception I...
5
by: shanti.miller | last post by:
I've got a grid that uses paging and displays a maximum of 10 records at a time. Sometimes the fields have a lot of data in them and the datagrid takes up way too much horizontal space. I've...
3
by: jcris25 | last post by:
pls help me on how to make a program by converting the day, month and year to a roman numerals... im looking forward..tnx and god bless...
7
by: daming23 | last post by:
Hi, I'm taking an online C++ class and having some difficulty understanding the assignments. Can someone please look at the code I wrote to ensure validity? I am not asking for the answer just some...
3
by: sweetamy | last post by:
i am making an html form......it should check the values before accepting the data in the database,,,,,,,,,,,,,a field name USER NAME should check if the value entered is only a character value nd...
22
by: kotlakirankumar | last post by:
please help me out the program for converting the integers to roman numerals using files in the c language from 1-5000 range send the program to my mail id ::::::: kotlakirankumar@gmail.com...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.