LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-20-2008, 10:29 PM   #1
andrw123
Member
 
Registered: Aug 2008
Distribution: Slackware 12.1
Posts: 35

Rep: Reputation: 15
C++: user input to an int array?


I am trying to have the user input an integer(ex.1010101010) and have it stored in an array. It could be stored in anything. I just need to have access to each individual digit to preform arithmetic on.


Any help will greatly appreciated.
 
Old 09-20-2008, 11:17 PM   #2
mjmwired
Member
 
Registered: Apr 2004
Distribution: CentOS6, CentOS5, F16, F15, Ubuntu, OpenSuse
Posts: 620

Rep: Reputation: 39
You can either capture the input as an int or as a char* string. If you capture as a string, then automatically every digit is a separate element in a char array.
 
Old 09-20-2008, 11:23 PM   #3
nadroj
Senior Member
 
Registered: Jan 2005
Location: Canada
Distribution: ubuntu
Posts: 2,539

Rep: Reputation: 60
as stated, you can either accept as an int or char array. if you want access to the individual elements to perform arithmetic on then you either have to convert that single character to a null-terminated string and use the "atoi" function or subtract the character (not integral number) zero from it, ie: x - '0'

another alternative to accessing the single integral values would be to use a loop and use a combination of "modulus 10" and "divide 10" to get the individual digits of a number.
 
Old 09-21-2008, 12:29 PM   #4
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
Quote:
Originally Posted by nadroj View Post
as stated, you can either accept as an int or char array.
Or a C++ string!
 
Old 09-21-2008, 02:30 PM   #5
andrw123
Member
 
Registered: Aug 2008
Distribution: Slackware 12.1
Posts: 35

Original Poster
Rep: Reputation: 15
I forgot to mention that I am a newbie at C++.

If I accept it as a string or char array(which I think are the same thing) then I can't preform arithmetic on it. It will not let me input into an int array, using cin. If I use atoi to convert it into an int then when I use cout to see what the int array is, it shows it as a very large hex number. nadroj- I'm not sure what you mean by using a loop.

The input will consist of a number with a various amount of digits.
 
Old 09-21-2008, 05:59 PM   #6
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
Can you post you code. That way it will be easier for people to give you further assistance and to explain what is going on.
 
Old 09-21-2008, 06:12 PM   #7
mjmwired
Member
 
Registered: Apr 2004
Distribution: CentOS6, CentOS5, F16, F15, Ubuntu, OpenSuse
Posts: 620

Rep: Reputation: 39
Quote:
Originally Posted by andrw123 View Post
I forgot to mention that I am a newbie at C++.
Please don't take this the wrong way, but I think it would be more beneficial to start with a book or some sets of tutorials online for C++.
 
Old 09-21-2008, 08:32 PM   #8
andrw123
Member
 
Registered: Aug 2008
Distribution: Slackware 12.1
Posts: 35

Original Poster
Rep: Reputation: 15
Well... I don't have the code. I have tried many different things, but all have failed. I didn't think having the code was necessary, since all I want to do is input an integer into an array.

Believe me, starting this thread was a last resort. I have went through two books and about every C++ tutorial on the web. I thought it was a very simple task. I search many different forums. I spent about about 2-3 hours a night for a week searching for how to do this.
 
Old 09-21-2008, 09:50 PM   #9
nadroj
Senior Member
 
Registered: Jan 2005
Location: Canada
Distribution: ubuntu
Posts: 2,539

Rep: Reputation: 60
here is an example of the first suggestion i had:
Code:
#include <iostream>
#include <cstdio>

using namespace std;

int main()
{

	string strNum;
	cin >> strNum;

	int i;

	for (i = 0; i < strNum.length(); i++)
	{
		int temp = strNum.at(i) - '0';
		cout << "digit is " << temp << ", digit*2=" << temp*2 << endl ;
	}

	return 0;
}
for each character in the input string, the character zero is subtracted from it, which will give its decimal value which is what you want. note that the input string is assumed to be an integer number.
edit: the above code illustration is to show that the individual characters (digits in this case) can be accessed and arithmetic can be performed on them.

there is no primitive datatype in c (or cpp, or java) that stores a number as an 'array' so that you can access the individual digits in that number. also i dont know of any other standard datatype that does this (sure, one may exist but if it did then you should have found it in your research).

are you trying to make 'really big' numbers and allow for algebraic operations on them? this seems to be a likely reason for wanting to do what your trying to do. this is different from wanting to do operations on the individual digits (independently). for example, you may want to do operations on the array of digits as a whole, such as '1','9' + '1' = '2','0' or you may want '1','9' + '1' = '1','10' or '1','0'. please narrow down which one your talking about. for the large number case, i thought java had this, so maybe look into that to see if there is a similar one for C++, or just look at its implementation details to see if you can make your own.


aside: regarding the looping suggestion, it was the alternative to the one i gave above (subtracting '0'). lets say we accept integer input ('int x') and the user types '567'.
loop 1: if you do 'x % 10' which is read 'x mod 10' the result is 7 (the least significant digit of x). now 'x / 10' gives x = 56.
loop 2: 'x % 10' = 6, the 2nd least significant digit of x. 'x / 10' gives x = 5.
loop 3: 'x % 10' = 5, the 3rd .... 'x / 10' gives x = 0;
loop 4: x == 0 so there are no more digits; stop.

Last edited by nadroj; 09-21-2008 at 10:12 PM.
 
Old 09-22-2008, 12:07 AM   #10
jaysrikrishna
LQ Newbie
 
Registered: Oct 2007
Posts: 21

Rep: Reputation: 15
Check Solution in next post ..........

Last edited by jaysrikrishna; 09-22-2008 at 12:38 AM. Reason: posted wrong reply ..........
 
Old 09-22-2008, 12:37 AM   #11
jaysrikrishna
LQ Newbie
 
Registered: Oct 2007
Posts: 21

Rep: Reputation: 15
Ok ....... you can take char array its good so that you can check
whether input entered is 0 1 or something else .......

you can access the string in following away .....


string bin_str ;
int digit ;
for( i = 0 ; i < bin_str.length() ; i++) {
digit = bin_str[i] ; // digit is ASCII value of bin_str[i]
// check the input number is binary or not ....
if(digit < 47 | digit > 48)
cout << " Invalid digit " ;
}
 
Old 09-22-2008, 10:17 PM   #12
andrw123
Member
 
Registered: Aug 2008
Distribution: Slackware 12.1
Posts: 35

Original Poster
Rep: Reputation: 15
I was wanting to operate on each individual digit. Your(nadroj) code did the trick. I just used another loop to put each digit, one by one, into the array.

jaysrikrishna- Your code will come very handy as well. I think you actually solved another problem I had.

Thanks for the help.

regards,
Andrew
 
Old 09-22-2008, 11:55 PM   #13
jaysrikrishna
LQ Newbie
 
Registered: Oct 2007
Posts: 21

Rep: Reputation: 15
hi andrew ....... its just for fun .....

digit = digit - 47 // this will give you back the binary 0 or 1 as integer

The code is exactly similar to " nadroj " just looks is different


Enjoy ......

Last edited by jaysrikrishna; 09-22-2008 at 11:56 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to get only int input darkangel29 Programming 1 09-09-2008 04:37 AM
invalid types ‘int[int]’ for array subscript medha Programming 16 08-25-2006 08:30 AM
passing int array to thread? Thinking Programming 2 09-21-2005 11:00 AM
invalid types int[int] for array subscript scuzzman Programming 2 11-16-2004 09:34 PM
Char array to int without losing value ? Dimitris Programming 3 01-14-2004 12:08 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:33 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration