Thread: What's wrong with my program?

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    7

    Question What's wrong with my program?

    Ok, I'm not completely sure what I'm doing wrong as I'm semi-new to programming. Any help would be much appreciated. Is it something with the strrev? not sure if I'm allowed to use it.


    Code:
    #include <stdio.h>
    #include <string.h>
    #include <conio.h>
    
    int 
    main(void)
    
    {
    char words[50][20];
    char *reversed;
    int i = 0;
    
    printf("=> ");
    scanf("%s", &words[i]);
    
    for(i = 0; i < 50; i++);   
        {
              printf("%s ", words[i]);
              if(words[i][0]==';')    
              break;
        }
              reversed = strrev(words[i]);
              printf("%s ", reversed);
               
               getch();
               return (0);
    }

  2. #2
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    I'm not completely sure what you're doing wrong, either. This might have something to do with one or more of the following:
    1. You didn't tell us what the program is supposed to do.
    2. You didn't tell us what problems you were having with the program.
    3. You are using a non-standard header file (conio.h).
    However, I can venture a guess. Right now, your for-loop looks like this:
    Code:
    for(i = 0; i < 50; i++);   
        {
              printf("%s ", words[i]);
              if(words[i][0]==';')    
              break;
        }
    Maybe you really wanted it to look like this?
    Code:
    for(i = 0; i < 50; i++);   
        {
              printf("%s ", words[i]);
              if(words[i][0]==';')    
              break;
        
              reversed = strrev(words[i]);
              printf("%s ", reversed);
        }
    EDIT: There's another obvious input error, but I can't help you with it unless you describe how the program should work in more detail.
    Last edited by pianorain; 12-03-2005 at 03:05 PM.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  3. #3
    Registered User
    Join Date
    Dec 2005
    Posts
    7
    Sorry about that, this program is supposed to take input from a user such as "apples and oranges" and then reverse the word order so the outcome would be "oranges and apples".

    I'm having a hard time figuring it out. Right now it runs like:

    input: dog
    output: god

    Also, I can't run it because it says my "break" statement is not in the loop, when it is I think. I updated my program to your post.

    conio.h is used with getch(); to keep the program from dying in Devc.

    Thanks
    Last edited by Infuriate; 12-03-2005 at 03:23 PM.

  4. #4
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Thats right your break statement is not in a loop
    Code:
    for(i = 0; i < 50; i++);
    remove the semicolon
    Kurt

  5. #5
    Registered User
    Join Date
    Dec 2005
    Posts
    7
    Thanks Kurt, now my program runs :P

    But, i need it to reverse the word order, right now when i type in cat and dog, it outputs cat tac and then a bunch of messed up weird characters.

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Try isspace().
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    Even though I don't know for certain what strrev() does, I can tell you that you do not need it for this program.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    strrev reverses a string ("hippo" -> "oppih"), which is not what you want.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

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