sdss

sdsd
Last edited on
Use while loop to separate integer to it's digits, and then output them.
HINT: Use / and % ( division and modulus) operators.

As for second... NO.

Happy coding!
The second part first: No, there is no way to declare a variable, and set it's value through a cin statement in one line.

The first part: Use the modulus operator (%) to remove from the left of what you want, and division to remove from the right hand side. Alternatly, you can convert it to a string, then just pick by index.

int x = 1234
int y = x%100 //34
int y /= 10 //3
Those aren't "parts" of the variable - 1234 is just one of an infinite number of possible ways of representing the number that you mean.

As to the second part - no, and I really, really don't see how that would be "easier".
nope
Last edited on
No you can't. You could do that if you'd input the number as a string, but the concept of "digits" doesn't exist for a number. it's just something we use to write numbers down.

For example, I could define a base 1234 digit system in which the number 1234 would look like this:
10

What we normally use is a base 10 digit system (which means that each digits represents a power of 10 ( for example, 352 would be 3*10^2+5*10^1+2*10^0), other commonly used systems would be base 2 (binary numbers), base 8 (octal numbers) or base 16 (hexadecimal numbers).

You see, there is no link between the number and it's representation as digits, so asking a computer what the "first" digit of 1234 is wouldn't make very much sense (well, since computers store data internally as binary numbers, the "first" digit for a computer would be an 1, because 1234 is stored as 10011010010).
Last edited on
So I could cast it as a string and then try to access the numbers that way? btw not looking for the code to do it, thats my job to figure out once I know if it's possible to do. Hints are greatly appreciated!
Cast? No. Convert, yes. How you do that depends on whether you're use C or C++ - in C you would use sprintf, in C++ you would use a stringstream. Or you could just use the mathematical approach that Intrexa gave you.
Um guys, as for the second question in the OP's origional post...
 
char Number = std::cin.get();

Or am I missing something? I agree with all of you that it wouldn't necessarily be easier but it IS possible. Even more so if you're familiar with 'streambuf' objects, but this is the beginners section.
I am a 100% sure he meant something like:
 
cin>> int a;
Ah, you're probably right that the OP wanted to use the extraction operator. I just don't like words like impossible, they denote limitations that aren't there.
I meant that what he wanted to do doesn't work... of course if he did something like
1
2
3
4
5
6
7
8
9
10
11
int getIntFromStream(istream& is)
{
     int result;
     is>>result;
     return result;
}

int main()
{
     int a = getIntFromStream(cin);
}


it would work, but I was just pretty sure that isn't what the OP is asking.
Last edited on
Topic archived. No new replies allowed.