Thread: Make the loop stop after print all character?

  1. #1
    Registered User
    Join Date
    Jul 2011
    Posts
    11

    Make the loop stop after print all character?

    Code:
    #include <stdio.h>
    
    main()
    {
    	char linetext[12];
    	int i;
    
    	printf("Enter a line of text:");
    	scanf("%[^\n]", &linetext);
    
    	for(i=0;i<10;i++)
    		printf("%c", linetext[i]);
    	printf("\n");
    
    }
    From the above code, I'm trying to figure out what I can do so that the loop will stop printing the character after null, so that no rubbish will come out behind null character.
    I'm a newbie, I tried to solve it, but I'm out of idea.
    If you don't want to show any example, do provide some hint (not the hard one), I'll try it, example do appreciate though.

  2. #2
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    try a while loop ex. while(linetext[i] != '\0')
    print
    i++;
    Last edited by camel-man; 09-22-2011 at 08:23 AM.

  3. #3
    Registered User
    Join Date
    Jul 2011
    Posts
    11
    Quote Originally Posted by camel-man View Post
    try a while loop ex. while(not '\0')
    I see, haven't figure out add (') between \0...
    I tried another method though, doesn't change to while loop, hope you don't mind...
    here is the code segment:
    Code:
    for(i=0;i<10;i++)
    		if(linetext[i] == '\0')
    			break;
    		else
    		printf("%c", linetext[i]);
    Is this more longer than what you'd mean?

    Edit: Ok it worked, thanks for the info, working on more difficult one now.
    Last edited by Blane; 09-22-2011 at 08:29 AM.

  4. #4
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by Blane View Post
    Code:
    #include <stdio.h>
    
    main()
    {
    	char linetext[12];
    	int i;
    
    	printf("Enter a line of text:");
    	scanf("%[^\n]", &linetext);
    
    	for(i=0;i<10;i++)
    		printf("%c", linetext[i]);
    	printf("\n");
    
    }
    From the above code, I'm trying to figure out what I can do so that the loop will stop printing the character after null, so that no rubbish will come out behind null character.
    I'm a newbie, I tried to solve it, but I'm out of idea.
    If you don't want to show any example, do provide some hint (not the hard one), I'll try it, example do appreciate though.
    Ok, so a couple of things to note:
    1. How to define main-FAQ()
    2. Your scanf function is unsafe since you didn't provide a width parameter. Read through: How to get a line of text from the user-FAQ


    Additionally, I am not sure what you are using to learn C, but I would suggest taking a look at our C Made Easy Tutorials. Those will get you going in the right direction.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Blane View Post
    Code:
    #include <stdio.h>
    
    main()
    {
    	char linetext[12];
    	int i;
    
    	printf("Enter a line of text:");
    	scanf("%[^\n]", &linetext);
    
    	for(i=0;i<10;i++)
    		printf("%c", linetext[i]);
    	printf("\n");
    
    }
    From the above code, I'm trying to figure out what I can do so that the loop will stop printing the character after null, so that no rubbish will come out behind null character.
    I'm a newbie, I tried to solve it, but I'm out of idea.
    If you don't want to show any example, do provide some hint (not the hard one), I'll try it, example do appreciate though.
    Look up strlen() in your library documentation...

  6. #6
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by CommonTater View Post
    Look up strlen() in your library documentation...
    or scanf for that matter....... Good Morning Tater!
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to make a program stop after a while
    By xiaohuai in forum C++ Programming
    Replies: 4
    Last Post: 06-21-2011, 01:19 AM
  2. Make accept() stop
    By b00l34n in forum Networking/Device Communication
    Replies: 28
    Last Post: 12-20-2004, 06:50 PM
  3. how to make a program stop indeffinitely?
    By BrianK in forum C++ Programming
    Replies: 4
    Last Post: 10-29-2002, 12:51 AM
  4. how to make it stop
    By xlordt in forum C Programming
    Replies: 6
    Last Post: 01-21-2002, 07:43 AM
  5. Make it stop!
    By dougaerb in forum C++ Programming
    Replies: 2
    Last Post: 10-31-2001, 11:04 PM