[Help]always wrong at first time

This is a discussion on [Help]always wrong at first time within the C Programming forums, part of the General Programming Boards category; I always receive wrong result at first time i run my program. When i type Hello Hello Hello , it ...

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    16

    Question [Help]always wrong at first time

    I always receive wrong result at first time i run my program. When i type Hello Hello Hello, it will dislay Hello Hello Hello and a symbol. But it only appear at the first time.

    You can run my code in Dev C to see.

    Code:
    #include<stdio.h>
    //#include<conio.h>
    #include<stdlib.h>
    #include<string.h>
    
    int main()
    {
       unsigned int choice, i, j, n, count;
       char c;
       char s1[100], s2[100], *s3;
       while(1){
       do{
           system("cls");
           printf("Choose one of the following options: ");
           printf("\n1. Input a string");
           printf("\n2. Dislay a string");
           printf("\n3. Convert to Upper Case");
           printf("\n4. Check the number of each charactor exists");       
           printf("\n0. Exit");
           printf("\nYour selection (0 -> 4): ");
           scanf("%d", &choice);}
       while(choice<0 || choice>4);
       switch(choice){
                         case 1: fflush(stdin);
                                 printf("\nEnter string ended by EOF (Enter -> Ctrl+Z -> Enter) : ");
                                 for(i=0; (c=getchar()) != EOF; i++) s1[i]=c;
                                 n=i;
                                 for(i=0;i<n;i++)
                                    if (s1[i]=='\n') 
                                        {
                                           for(j=i;j<n;j++) s1[j]=s1[j+1];
                                           s1[n--]='\0';
                                           i--;
                                        };
                                 system("pause");
                                 break;
                         case 2: printf("String entered is:");
                                 puts(s1);
                                 system("pause");
                                 break;
                         case 3: strcpy(s2,s1);
                                 s3=strupr(s2);
                                 printf("String coverted to upper case: ");
                                 puts(s3);
                                 system("pause");
                                 break;
                         case 4: for (i = 0;i<n;i++)
                                    if ( (s1[i] >='a' && s1[i] <= 'z') || (s1[i]>='A' && s1[i]<='Z') || (s1[i]>='0' && s1[i]<='9') )
                                    {
                                       int count = 1 ;
                                       for (j = i+1;j<n;j++)
                                          if (s1[j] == s1[i])
                                          {
                                             s1[j] = ' ' ;
                                             count++ ;
                                          }
                                       printf("Have %d letter %c in the string\n",count, s1[i]);
                                    }
                                 system("pause");
                                 break;
                         case 0: exit(0);}}
    }

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Close to this:Similar topic.

    Should probably be kept there... That way if you took us seriously, you would know that your code isn't portable. Not everyone is able to test your code on their system because it relies on extensions and/or implementation behavior.

    Slight changes in program purpose between the two threads, but the general badness of both programs is there.

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by kingofdcp View Post
    You can run my code in Dev C to see.
    No one will ever give you a job to write code that only runs on Dev C, so if you are in school with the intent of becoming some kind of professional, you should just get over it right now.

    It was explained to you before that using fflush(stdin) is improper and will result in undefined behavior. If you want to ignore that advice, why are you asking for more?
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    16

    Thumbs down

    Quote Originally Posted by MacGyver View Post
    Close to this:Similar topic.

    Should probably be kept there... That way if you took us seriously, you would know that your code isn't portable. Not everyone is able to test your code on their system because it relies on extensions and/or implementation behavior.

    Slight changes in program purpose between the two threads, but the general badness of both programs is there.
    Thk for comment but problem in this code is different. I put fflush(stdin) but the problem does not solved.
    Difference is in this code i use puts(s1); to print string intead use for(i=0;i<n;i++) printf("%c", s1[i]); like that code.

    Quote Originally Posted by MK27 View Post
    No one will ever give you a job to write code that only runs on Dev C, so if you are in school with the intent of becoming some kind of professional, you should just get over it right now.

    It was explained to you before that using fflush(stdin) is improper and will result in undefined behavior. If you want to ignore that advice, why are you asking for more?
    Yap man. I say " run on Dev C" only mean: you can run to see (not only in Dev C,it maybe BC) because my english is not good so i afraid of you can't understand my problem.
    Last edited by kingofdcp; 06-06-2009 at 06:33 AM.

  5. #5
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,183
    I put fflush(stdin) but the problem does not solved.
    The problem is, you SHOULDN'T.

  6. #6
    Registered User
    Join Date
    May 2009
    Posts
    16
    Quote Originally Posted by cyberfish View Post
    The problem is, you SHOULDN'T.
    i removed but not solved yet ! If i still use:

    for(i=0;i<n;i++) printf("%c", s1[i]); to print intead use puts(s1) it run without error

  7. #7
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    As noted fflush(stdin) is undefined behavior since fflush() is used only for output streams.
    Stop printing string when its end ie NULL char is reached instead of walking all the way to its last index.

  8. #8
    Registered User
    Join Date
    May 2009
    Posts
    16
    Quote Originally Posted by itCbitC View Post
    As noted fflush(stdin) is undefined behavior since fflush() is used only for output streams.
    Stop printing string when its end ie NULL char is reached instead of walking all the way to its last index.
    so in my case, can't use puts ? if yes, pls tell me how to fix? thks

  9. #9
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    Which is the one that's not working for you? The for loop or puts().

  10. #10
    Registered User
    Join Date
    May 2009
    Posts
    16
    Quote Originally Posted by itCbitC View Post
    Which is the one that's not working for you? The for loop or puts().
    as i said. That is puts() in case 2 and case 3. The string is added more 1 symbol at the first time. (only first time). I dont know why that.

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    1 - Try initializing your arrays at creation time:
    Code:
    char s1[100] = {0}, s2[100] = {0};
    2 - Try checking for \r as well as \n and removing them both. Normally you wouldn't actually see the symbol for \r, but you might consider checking for it as well.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Weird Times I'm getting
    By afflictedd2 in forum Linux Programming
    Replies: 8
    Last Post: 07-23-2008, 07:18 AM
  2. Read and set\change system time
    By Hexxx in forum C++ Programming
    Replies: 9
    Last Post: 01-02-2006, 06:11 AM
  3. Problem with loop stopping at wrong time
    By Baron in forum C Programming
    Replies: 11
    Last Post: 12-14-2005, 07:35 PM
  4. Pointer problem... i think
    By fatdunky in forum C Programming
    Replies: 6
    Last Post: 11-30-2005, 02:45 PM
  5. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 11:01 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21