Using getchar to populate an array

After just getting my head round using scanf to read from the keyboard and populate an array, using ints. I now have to do this:

1. Create an array with 8 characters using the getchar function (a word of your choice) populated by the keyboard. This will be your first array.
2. Create a second array with another 8 character word using the same function.
3. Modify the program so that the contents of the first array are transposed to the second array and likewise the second array contents are transposed to the first array.

I've done this EXACT same task for integers using scanf and int arrays, see:

http://pastie.org/3910089

The code is really crap (I know) and a bit of a joke ;-) but it does the job.

After getting some help elsewhere I have come up with this to begin with:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int main()

{
    char c;
    char word1[8];
    char word2[8];
    int counter = 0;
    
    {
         while (counter < 8)
         {
              
              c = getchar();
              word1[counter] = c;
              counter++;
               
               }
               }
               }


But this doesn't work, it seems to be skipping ahead in within that loop for some reason, can someone tell me why?
Last edited on
What exactly does not work?
Sorry... I can't really explain.

http://img850.imageshack.us/img850/9747/consolescreenshot06.jpg

If you look at that screenshot; I enter 'a' for location 0 then it skips location 1 and goes straight to location 2. It looks like it's incrementing 'counter' too many times or something... there must be something wrong with my loop but I just don't know.
I have not seen your screenshot but your code is valid. You should enter a string of 8 characters and after that press Enter key.
closed account (4z0M4iN6)
Maybe this code could be helpful?

1
2
3
4
5
6
7
8
9
	char input[9];
	char c;

	int i=0;
	while( (c = getchar()) != '\n')
	{
		if(i < 8) input[i++] = c;
	}
	input[i] = 0;


Don't know, wether you need a char string, which should be ended by '\0'
I thought I had it but for some reason it's not happening. Does this code look okay to you?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
printf("Enter First Word (8 Characters): ");
         while (counter < 8)
         {              
              c = getchar();
              word1[counter] = c;
              counter++;
              }
         
         printf("Enter Second Word (8 Characters): ");           
         while (counter2 < 8)
         {
               b = getchar();
               word2[counter2] = b;
               counter2++;
               }



And what would be the proper way to print the arrays once they have been populated using printf?
Taking into account that all variables were properly initialized I do not see any error except that if between inputs of the two words the Enter key was pressed then it was included as a character in the second word.
I was trying to figure out what was going on!

Thank you vlad, can you tell me where the error is in that code above and how to fix it?

Edit: How can I change the code so that enter is not necessary to be pressed?
Last edited on
When you are entering the characters at the keyboard, are you pressing the "return/enter" key for each one? If so, the getchar() function is returning a carriage return character representing that keystroke. Your program was doing exactly what is was coded to do, which wasn't really what you had in mind. Take a close look at the example that dadabe posted, and try to work out what it is supposed to do.

Regarding using printf to print your character array:
Declare your character array one element longer than the number of characters that you need. Set the last element to zero (the null terminator).

Look up the printf function (you can use the reference available here).
The "proper way" would be to use a format string containing the %s specifier, and pass the address of your array as an argument. You can use printf to print your array without a format string, but if the user has entered format specifiers, it can lead to undesired effects.
Last edited on
closed account (4z0M4iN6)
I could say:

I suggest really, you should look at my example. Normally I don't show any code, but only give ideas. In your case, looking at my code is neccessary. I had a long discussion with others, who dumped the posts of users full with their listings. And I told them, not to do this. Do you think, I would have liked, to show you my code? I made no special version for beginners. It's an example with a clear idea, and written, as a professional would write it. So you need to use your brains, to grasp it. If I've made it too difficult, let me know.

But I could say also:

What would you like more, a listing, which solves your problem, or an idea, which solves the problem?

What do you think of this idea?

Why do you do a loop until 8. Wouldn't it better to do a loop until '\n' ?
Inside this loop you can count. For counter < 8 you store, if bigger, you don't store.

And after you have done this, you could also have a look at my example.
Last edited on
Topic archived. No new replies allowed.