Click here to Skip to main content
15,891,607 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Hello
Im learning c at the moment and run into a problem. Would be great if someone could help me as its a very basic problem.

Im trying to write 10 strings (got from scanf) in a 2 dimensional array of char:

C++
char wortListe[10][10];

				for ( y=0;y<10;y++)
			{
				for ( i=0;i<10;i++)
				{
					scanf_s("%c",&wortListe[i][y]);
				}
				
			}


and read it out with

C++
while ( i<10 )
			{
				while ( y<10 )
				{
					printf_s("%c",wortListe[i][y]);	
					y++;
				}
				i++;
			}
			break;


but its not working for some reasons. Normaly im doing everything i need with delphi so would be great if someone could point at the errors ive made :)
Posted
Comments
Sergey Alexandrovich Kryukov 14-Nov-13 20:33pm    
What is "not working", exactly? What are initial values of i and y in the second code fragment? show the line when they are declared/initialized. Do you share the same i and y objects in both code fragments?
Did you use the debugger?
—SA
Marco Bertschi 15-Nov-13 4:21am    
Please, can you post what the above code is printing? Any errors, warnings during Build?

See http://www.cplusplus.com/reference/cstdio/scanf/[^]. The description for the format specifier c states that:
The next character. If a width other than 1 is specified, the function reads exactly width characters and stores them in the successive locations of the array passed as argument.

(Emphasis added)

In other words, rather than passing a variable (or array element) of type char, you should pass a variable of type array of char to scanf, like this:
C++
scanf("%c", wortliste[i]+y);

Of course, you might as well replace the inner loop by reading all 10 characters at once, using the width specifier:
C++
scanf("%10c", wortliste[i]);
 
Share this answer
 
you are using the wrong identifier for the job: use "%s"

scanf_s("%s",wortListe[i]);

printf_s("%s",wortListe[i]);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900