473,233 Members | 1,414 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,233 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 4951
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...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.