simple char array
-
Hello all. I am programming a simple program in win32 console. #include <stdio.h> #include <conio.h> main() { int i,j; char list[80][20]; for(j=0;j<5;j++) { for(i=0;i<50;i++) { list[i][j]=getchar(); if (list[i][j]=='\n') break; } } for(j=0;j<5;j++) { for(i=0;i<50;i++) { putchar(list[i][j]); } } } It should give some char and then print them. but when it want to print some other things (&%^&*vjc) print.
-
Hello all. I am programming a simple program in win32 console. #include <stdio.h> #include <conio.h> main() { int i,j; char list[80][20]; for(j=0;j<5;j++) { for(i=0;i<50;i++) { list[i][j]=getchar(); if (list[i][j]=='\n') break; } } for(j=0;j<5;j++) { for(i=0;i<50;i++) { putchar(list[i][j]); } } } It should give some char and then print them. but when it want to print some other things (&%^&*vjc) print.
please read the forum guidelines ("how to ask a question"), use PRE tags, ask a specific question and provide symptoms if it does not do as you would like. Suggestion: If it does not work, start with something simpler; do not enter 20 lines of code if you are not experienced enough to get the first 5 to work as expected. And do you really want to enter up to 250 characters one by one? :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read formatted code with indentation, so please use PRE tags for code snippets.
I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).
-
please read the forum guidelines ("how to ask a question"), use PRE tags, ask a specific question and provide symptoms if it does not do as you would like. Suggestion: If it does not work, start with something simpler; do not enter 20 lines of code if you are not experienced enough to get the first 5 to work as expected. And do you really want to enter up to 250 characters one by one? :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read formatted code with indentation, so please use PRE tags for code snippets.
I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).
What should I do? Can you help me on this method...
-
What should I do? Can you help me on this method...
it is not clear what you want. it is clear this is one of your very first attempts at programming; if it does not work, start with something simpler. Do not start with 2-dimensional arrays, until you know everything there is to know about one-dimensional ones. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read formatted code with indentation, so please use PRE tags for code snippets.
I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).
-
Hello all. I am programming a simple program in win32 console. #include <stdio.h> #include <conio.h> main() { int i,j; char list[80][20]; for(j=0;j<5;j++) { for(i=0;i<50;i++) { list[i][j]=getchar(); if (list[i][j]=='\n') break; } } for(j=0;j<5;j++) { for(i=0;i<50;i++) { putchar(list[i][j]); } } } It should give some char and then print them. but when it want to print some other things (&%^&*vjc) print.
Im only guessing but.. Look at comments in the code. Once you press enter, it exits the nested
for
loop and prints out what you put in.#include <stdio.h>
#include <conio.h>int main()
{int i,j; char list\[80\]\[20\]={'\\r' }; printf("\\n input some chars ..\\n\\n "); for(j=0;j<1;j++) { for(i=0;i<50;i++) { list\[i\]\[j\]=getchar(); //input , press enter to exit getch's internal loop printf("%c = %d ",list\[i\]\[j\],(int)(list\[i\]\[j\]) ); if ( (int) (list\[i\]\[j\]==10) ) { printf("\\n User pressed Enter: new line .. "); break; // char code for \\n is 10 } //if ( (int) (list\[i\]\[j\]==10) ) continue; } } printf("\\n\\nListing..\\n"); for(j=0;j<5;j++) { for(i=0;i<50;i++) { printf("%c",list\[i\]\[j\]); //output } printf("\\n"); } printf("\\n"); return 0;
}
......
-
Hello all. I am programming a simple program in win32 console. #include <stdio.h> #include <conio.h> main() { int i,j; char list[80][20]; for(j=0;j<5;j++) { for(i=0;i<50;i++) { list[i][j]=getchar(); if (list[i][j]=='\n') break; } } for(j=0;j<5;j++) { for(i=0;i<50;i++) { putchar(list[i][j]); } } } It should give some char and then print them. but when it want to print some other things (&%^&*vjc) print.
When you create your arrays you're not initialising the contents to anything. Have a look at the rules for initialising automatic arrays in C and you might see what's wrong. Get in the habit of initialising all variables as you declare them, it'll save loads of tears. Cheers, Ash
-
Hello all. I am programming a simple program in win32 console. #include <stdio.h> #include <conio.h> main() { int i,j; char list[80][20]; for(j=0;j<5;j++) { for(i=0;i<50;i++) { list[i][j]=getchar(); if (list[i][j]=='\n') break; } } for(j=0;j<5;j++) { for(i=0;i<50;i++) { putchar(list[i][j]); } } } It should give some char and then print them. but when it want to print some other things (&%^&*vjc) print.
Since you stop collecting inputs at first newline but then ouput the whole array content, some resulting garbage is inevitable, I suppose. :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles]