473,406 Members | 2,356 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,406 software developers and data experts.

help with user input into arrays

hey ive been messing around with this program to take a user inputed number, put it into an array and then reverse the number. I also want it to omit unecessary 0's at the begging.

for example if the user inputs 100 the reverse shows 1, becuase with 001 the leading zeros are unecessary.

I finally did it a complicated way and was wondering if there is an easier way to get the user inputed number into the array, and prefferably into an int array not a char.
heres my code any help is greatly appreciated...

Expand|Select|Wrap|Line Numbers
  1.  
  2. #include <iostream>
  3.  
  4. void reverse(char input[]){
  5.  
  6.     int c=0;
  7.     for (int x=0; input[x]!='\0' ; x++)
  8.         c++;
  9.     while (true){
  10.         if (input[c-1] !='0'){    
  11.             for(c; c >= 0; c--)
  12.                 cout << input[c-1];
  13.  
  14.             cout << endl;
  15.             break;
  16.             }
  17.         else
  18.             c--;
  19.         }
  20. }
  21.  
  22. int main(void)
  23. {
  24.    char input[256];
  25.  
  26.     cout << "Please enter a postive integer: ";
  27.     cin.getline (input,256);
  28.  
  29.     reverse (input);
  30.  
  31.    return 0;
  32. }
  33.  
thanks

Rod
Nov 7 '06 #1
1 3595
horace1
1,510 Expert 1GB
you can use strlen() to get the number of character in the string and you can test for leading 0's in the while(), e.g.
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <iomanip>
  3. using namespace std;
  4.  
  5. void reverse(char input[]){
  6.     int c=strlen(input)-1;
  7.     while (input[c] =='0') if (--c < 0) { cout << "0\n"; return; }
  8.     for(c; c >= 0; c--)
  9.             cout << input[c];
  10.     cout << endl;
  11. }
  12.  
  13. int main(void)
  14. {
  15.    char input[256];
  16.     cout << "Please enter a postive integer: ";
  17.     cin.getline (input,256);
  18.     reverse (input);
  19.    cin.get();cin.get();
  20.    return 0;
  21. }
  22.  
it will print a 0 for all 0000's input
Nov 7 '06 #2

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

Similar topics

8
by: Foxy Kav | last post by:
Hi everyone, Im currently doing first year UNI, taking a programming course in C++, for one project i have to create a simple array manipulator... that i have done, but i cant figure out how to...
7
by: x muzuo | last post by:
Hi guys, I have got a prob of javascript form validation which just doesnt work with my ASP code. Can any one help me out please. Here is the code: {////<<head> <title>IIBO Submit Page</title>...
6
by: Ant | last post by:
Hi, I'm trying to store some user input from a web form into an array, now the thing is I would like to store multiple users entries so I was thinking of using a multidimensional array so I...
8
by: baustin75 | last post by:
Posted: Mon Oct 03, 2005 1:41 pm Post subject: cannot mail() in ie only when debugging in php designer 2005 -------------------------------------------------------------------------------- ...
0
by: ward | last post by:
Greetings. Ok, I admit it, I bit off a bit more than I can chew. I need to complete this "Generate Report" page for my employer and I'm a little over my head. I could use some additional...
22
by: Amali | last post by:
I'm newdie in c programming. this is my first project in programming. I have to write a program for a airline reservation. this is what i have done yet. but when it runs it shows the number of...
2
by: Zerofury | last post by:
Okay this is what i'm attempting to do. I have to modify this program that i wrote so that it allows the user to list items by alpha as an option on the main menu. Here is my problem. If i sort the...
5
by: saytri | last post by:
Hi i have this project were i have to do a quiz. i wrote the questions in a textfile and i called them through java. I have also made a menu to choose which type of quiz. But before accessing the...
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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
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...

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.