473,657 Members | 2,753 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C++ Entering numerals of an integer horizontally

542 Contributor
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 4981
whodgson
542 Contributor
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 Recognized Expert Moderator Expert
It's the cin.get() echoing the enter key you used to enter the character.

Maybe you could try getchar().
Mar 3 '08 #3
Simonius
47 New Member
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 Recognized Expert Moderator Expert
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 Recognized Expert Contributor
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 Contributor
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 Contributor
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
3105
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 displayed in Arabic but the numbers were Latin. In other parts of my app ,Arabic numerals would appear as expected. I found an MSDN link to the following web page that mentioned there was an issue with the current DropDownList and ListBox ASP.NET...
13
4379
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
11760
by: Patrick Blackman | last post by:
How do you write regular numbers eg 1 23 475896 in roman numerals inC#
4
1182
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 would also like to add another if statement checking if 'date' has any
5
2846
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 tried setting the height and it doesn't do anything. I can set the width to some very large number like 3000px and that works ok, but it's a waste when the data doesn't require that much space. Is there a way to limit the height of the grid or of...
3
2139
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
14389
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 guidance, I’m totally lost. The assignment is as follow: Write a prg to convert numbers entered in Roman Numeral to decimal. Your prg should consist of a class, say romanType. An object of the type romanType should do the following: a. Store...
3
1681
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 no numerals,,,nd if numerals r entered it should raie an exception i.e. alert msg...........how is it done using java script.......... thanx
22
3199
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 thank you all
0
8382
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8297
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8717
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8600
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6162
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4150
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2726
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1930
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1600
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.