4 Replies - 242 Views - Last Post: 24 February 2013 - 02:54 PM Rate Topic: -----

#1 TheOne6152  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 11-February 09

How do I read user input (char) into an array?

Posted 24 February 2013 - 12:08 PM

I'm new to c++ and I am having trouble on taking a users input followed by a comma (ex: "hello,") and putting it into an array (array[0] = h, array [1] = e, etc.) using a loop, one char at a time. I know how to use arrays a bit, but I don't know how I would read in each char into the array with a loop. Any and all hep is greatly appreciated. Thank you.
Is This A Good Question/Topic? 0
  • +

Replies To: How do I read user input (char) into an array?

#2 TwoOfDiamonds  Icon User is offline

  • D.I.C Regular

Reputation: 54
  • View blog
  • Posts: 272
  • Joined: 27-July 12

Re: How do I read user input (char) into an array?

Posted 24 February 2013 - 01:20 PM

There is a function called getchar() in "stdio.h" ... you could take a look :)/>

It's right here .

This post has been edited by TwoOfDiamonds: 24 February 2013 - 01:23 PM

Was This Post Helpful? 0
  • +
  • -

#3 TheOne6152  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 11-February 09

Re: How do I read user input (char) into an array?

Posted 24 February 2013 - 02:49 PM

But how would I read each char into the array using a loop?
Was This Post Helpful? 0
  • +
  • -

#4 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1091
  • View blog
  • Posts: 4,572
  • Joined: 09-June 09

Re: How do I read user input (char) into an array?

Posted 24 February 2013 - 02:54 PM

Why are use not using scanf?
Was This Post Helpful? 0
  • +
  • -

#5 TwoOfDiamonds  Icon User is offline

  • D.I.C Regular

Reputation: 54
  • View blog
  • Posts: 272
  • Joined: 27-July 12

Re: How do I read user input (char) into an array?

Posted 24 February 2013 - 02:54 PM

it could be something like

int i=0;
do
{
  ch = getchar() ;
  charArray[i] = ch ;
  putchar(ch); // put this only if you want echo , that means 
              //if you want to display the char that the user enters
  i++;
}while (ch != '\0'); //or whatever condition you want 



This is the basic method .
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1