Thread: whats wrong of this program

  1. #1
    Registered User
    Join Date
    Aug 2003
    Posts
    10

    Unhappy whats wrong of this program

    Code:
    #include <stdio.h>
    int main()
    {
            int i;
            char input[80];
            printf("Enter a string please:");
            gets(input);
    
            printf("The words are:\n");
            for(i=0; input[i]<=80; i++)
            if(input[i]!=' ')
            printf("%c", input);
            else if(input[i]==' ')
            printf("\n");
    
    return 0;
    }
    the program breaks the string into parts
    but there is a problem, it stop in the for loop and print nothing....
    how can i solve this problem??

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Code:
            for(i=0; input[i]<=80; i++)
                {
                if(input[i]!=' ')
                     printf("%c", input);
                else if( input[i]==' ' )
                   printf("\n");
                }
    Your else statment was not inside the for loop.

  3. #3
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    And you can just use else, too. You don't need the 'if( input[ i ] == ' ' )' part.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  4. #4
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    You'd better hope no one else sees this. Switch to fgets for the string input. It's a better function because you specify how many bytes you want to read in therefore avoiding problems like buffer overflow. Also in your for loop you have the condition as being <= 80. Well your array is 80 elements [0-79] so you don't actually have an 80 index. Change it to a < and that should be good. Also try putting braces around your for loop to include the if and else. Good job with the code tags and using int main though.

    Edit:

    Actually your condition for your for loop is "input[i] <= 80" why are you checking the array? just use "i < 80"
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  5. #5
    Registered User
    Join Date
    Aug 2003
    Posts
    10
    thx.but can i base on this stupid program to have a 2d array and break the string into words?

  6. #6
    Registered User Draco's Avatar
    Join Date
    Apr 2002
    Posts
    463
    The strtok() function will make breaking up the words an easy job for you. This url will take you to the faq page that explains exactly what you want to do.

    http://faq.cprogramming.com/cgi-bin/...&id=1044780608

  7. #7
    Registered User
    Join Date
    Aug 2003
    Posts
    10
    thx!but can i do it in another way.
    i have tried one like this
    Code:
    #define maxsize 80
    #include <stdio.h>
    int main()
    {
            int i;
            char input[maxsize];
            char real[maxsize][maxsize];
            printf("Enter a string please:");
            gets(input);
    
            printf("The words are:\n");
            for(i=0; input[i]!='\0'; i++)
            {
            if(input[i]!=' ')
            real[i][i]=input[i];
            printf("%s\n", real);
            else
            printf("\n");
            }
    return 0;
    }
    but the output is that the program can only print the first character(if i enter hi hi, it prints h only)
    how can the program print
    hi
    hi
    ??
    Last edited by maniel; 08-20-2003 at 11:45 AM.

  8. #8
    Registered User
    Join Date
    Aug 2003
    Posts
    4
    Code:
    if(input[i]!=' ')
            real[i][i]=input[i];
            printf("%s\n", real);
            else
            printf("\n");
    your if statement only is doing the first command, while the printf will go all the time and the else isn't attached to the if statement.


    Btw add tabs to your code so you can catch this problem
    Code:
                  
      currently written as:
         if(input[i]!=' ')                        
                real[][]                                    
         printf                                                
         else                                      
                printf                         
    
    should be:
         if(input){
                 real[]
                 printf
          }
          else
    Use curly brackets for all the statements your if and else are using to make the code more readable


    -Addy
    Last edited by addisonp; 08-20-2003 at 04:55 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Maze Program - What am I doing wrong?
    By Gipionocheiyort in forum C++ Programming
    Replies: 20
    Last Post: 08-02-2007, 01:31 PM
  2. Replies: 5
    Last Post: 01-13-2007, 02:14 AM
  3. What is wrong with my code? My first program......
    By coreyt1111 in forum C++ Programming
    Replies: 11
    Last Post: 11-14-2006, 02:03 PM
  4. Whats wrong with this program - Output of a series
    By duffmckagan in forum C Programming
    Replies: 2
    Last Post: 07-26-2006, 09:57 AM
  5. Whats wrong with my program?
    By Ruflano in forum C++ Programming
    Replies: 5
    Last Post: 02-21-2002, 05:09 PM