Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. simple char array

simple char array

Scheduled Pinned Locked Moved C / C++ / MFC
data-structures
7 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    hasani2007
    wrote on last edited by
    #1

    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.

    L S A C 4 Replies Last reply
    0
    • H hasani2007

      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.

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      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).


      H 1 Reply Last reply
      0
      • L Luc Pattyn

        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).


        H Offline
        H Offline
        hasani2007
        wrote on last edited by
        #3

        What should I do? Can you help me on this method...

        L 1 Reply Last reply
        0
        • H hasani2007

          What should I do? Can you help me on this method...

          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #4

          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).


          1 Reply Last reply
          0
          • H hasani2007

            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.

            S Offline
            S Offline
            Software_Developer
            wrote on last edited by
            #5

            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;
            

            }

            ......

            1 Reply Last reply
            0
            • H hasani2007

              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.

              A Offline
              A Offline
              Aescleal
              wrote on last edited by
              #6

              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

              1 Reply Last reply
              0
              • H hasani2007

                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.

                C Offline
                C Offline
                CPallini
                wrote on last edited by
                #7

                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]

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups