We're a community of 1.1M IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,080,534 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Reading integer in an array

Hi, I am a beginner and new here. I have a question which might be very easy for you all so thought of asking a good solution.
I need to read an integer from console into an array in c++ , can any one please help!
for example: I want output should be like
Enter number
user enters: 1234
the contents should get stored in an array as a[0]=1 ,a[1]=2,a[2]=3,a[3]=4

Any idea on this would be a great help, either in the form of one liner code or construct that I think I am missing because I am new to ths language.

Many thanks

3
Contributors
4
Replies
22 Hours
Discussion Span
8 Months Ago
Last Updated
5
Views
Question
Answered
pooja123
Newbie Poster
3 posts since Oct 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

Start with the basics:

#include <iostream>
#include <string>

using namespace std;

int main()
{
  //code goes here

 return 0;
}
iamthwee
Posting Genius
6,378 posts since Aug 2005
Reputation Points: 1,567
Solved Threads: 489
Skill Endorsements: 35

Hi I started up with this code block though,but stuck at some construct or language rule that I am missing

   int arr[20];
   char *input;
   input =new char[20];

 cout<<"enter  input number";
    cin>>input;//some constuct here to read number from console ??
for(int i=0;i<10;i++){
arr[i]=input[i];}

I want that if I enter number as 12345
then my variable 'input' should store this number as innput[0]=1..input[4]=5
Is there any way out for this and another thing i should know that user has hit 'Return key' so a construct to check that user input is finished.

Help is much appreciated
Thanks
pooja123
Newbie Poster
3 posts since Oct 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

So what's not working right? How can you tell?

when asking for help, you must tell us what's going on, not just say "it doesn't work". There are hundreds of ways it can fail. Which one is yours?

WaltP
Posting Sage w/ dash of thyme
Team Colleague
11,404 posts since May 2006
Reputation Points: 3,421
Solved Threads: 1,055
Skill Endorsements: 37
Question Answered as of 8 Months Ago by WaltP and iamthwee

It is solved now
Thanks

pooja123
Newbie Poster
3 posts since Oct 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page generated in 0.0697 seconds using 2.72MB