Thread: Help with reading input, I'm clobbering something

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    5

    Help with reading input, I'm clobbering something

    Hi, my problem is I want to read input from the keyboard and parse it into two separate int variables. I'm using a comma a delimeter and it works fine if I my entry is in the form,

    ##,##

    but I want to be

    ,##


    to imply 0,##

    Basically I'm scrolling through numbers starting at different places. What's driving me bonkers is everything works except for the ,## entries. What, drives me doubly bonkers is that I wrote the code in another program and pasted it into this one. All the variables are wired up and it even works for the ##,## entries. I;ve spent hours on this and am just not sophisticated enough to see the problem

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
    
    char divs[1024];
    int propDivs[1024];
    int i;
    int sum=0;
    
    
    char input[1024];
    int tot=0;
    int tot2 = 0;
    int mlt = 10;
    char num1[100];
    char num2[100];
    char comma=0;
    int com = 0;
    int sum1=0;
    int sum2=0;
    int count=0;
    int quitCheck = 0;
    char help[4];
    char quit[4];
    int sumprime=0;
    int sumperfect=0;
    int t1 = 0;
    int t2 = 0;
    int pfTog = 0;
    int apTog = 0;
    int prTog = 0;
    int checkQuit =0 ;
    
    while(checkQuit == 0){
    printf("Enter a number: ");
    
    fgets(input, sizeof(input), stdin);
    input[strlen(input)-1] = '\0';
    if((input[0]>= 48 && input[0]<=57)|| input[0] == ',' || input[0] == 'h' || input[0] == 'H'|| input[0] == 'r' || input[0] == 'R'||input[0] == 'c' || input[0] == 'C' ||input[0] == 'q' || input[0] == 'Q' || (input[0] >= 48 && input[0] <= 57)){
    //*************************************************************************************                      
    //***************QUIT QUIT QUIT QUIT QUIT QUIT QUIT************************************
    //*************************************************************************************
     if(input[0] == 'q' || input[0] == 'Q'){
            for(i=0; i<4; i++){
                 if(input[i] >= 65 && input[i] <= 90){                         
                      quit[i] = input[i]+32;
                 }else
                      quit[i] = input[i];
                 }
                      quit[4] = '\0';                    
                               
                 if(strcmp(quit,"quit")==0){
                      printf("\n***Session Terminated***\n");
                      checkQuit = 1;
                 }else{
                      printf("\nInvalid Entry\n");                           
     }
    } else   
    //PARSE NUMBERS#####################################################
    for(i=0;i<strlen(num2);i++){
    num2[i]='\0';                            
    }
     if(input[0] == ','){
          num1[0] = '0';
          num1[1] = '\0';
               for(i=1; i<strlen(input); i++){
                    num2[i]=input[i+2];
               }
               for(i=0; i<strlen(num1); i++){
                    sum1 = sum1*10+num1[i]-'0';
               }
               for(i=0; i<strlen(num2); i++){
                    sum2 = sum2*10+num2[i]-'0';
               } 
          printf("\nSum1 = %s\n",num1);
          printf("\nSum2 = %s\n",num2);
          printf("\nend comma\n");  
    //**********************************
    }   
     if(input[0]>= 48 && input[0]<=57){                           
          for(i=0; i<strlen(input); i++){
               if(input[i] == ','){
                    comma = i;  
               }
          }  
          for(i=0; i<comma; i++){         
               num1[i] = input[i];
               num1[i+1] = '\0';
          }
        for(i=0; i<strlen(input); i++){
             num2[i] = input[i+comma+1]; 
             num2[i+1] = '\0';
        }
    sum2=0;
    sum1=0;
     for(i=0; i<strlen(num1); i++){
         sum1 = sum1*10+num1[i]-'0';
     }
     for(i=0; i<strlen(num2); i++){
         sum2 = sum2*10+num2[i]-'0';
     }
     printf("\nSum1 = %d\n",sum1);
     printf("\nSum2 = %d\n",sum2);
     printf("\nend number\n");  
    }else// end input
     printf("\nERROR\n");
    
             }//end last for
     system("PAUSE");    }//end while
    }//end main

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You could think of the number line. In a for loop from strlen(input) to 0, read the next digit. multiply all previous digits in the number you're building X 10.

    What about removing the comma's and using the string to int function?


    Anyway, if neither suggestion helps you solve the problem, show a few examples of the input that is troublesome - actual digits, and what you need them to change to. I don't want to work with the ##,## because they are ambiguous.
    Last edited by Adak; 03-16-2011 at 04:16 PM.

  3. #3
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    What if you just used a regex pattern instead? Something like:
    (?<num1>[0-9]*),(?<num2>[0-9]+)

    Then you can just take num1 and num2 from the match.

    Also, looking through your code I see a lot of "magic numbers". Especially 48 and 57. '0' and '9' would make your intent more immediately clear.

    Another thing: You can clean up a lot of the code by replacing stuff like if(input[0] == 'q' || input[0] == 'Q') with if(toupper(input[0]) == 'Q').
    If you understand what you're doing, you're not learning anything.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Is there any particular reason you're hard coding numbers in there instead of using the alphabet?
    Code:
               for(i=1; i<strlen(input); i++){
                    num2[i]=input[i+2];
               }
    Is there any reason you are starting this at 1 instead of 0? You have already set num2[0] to '\0', then you skip up and start using num2[1], so any time you try to use num2 as a string from that point on is going to fail (not give you any output).


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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Am I reading User input into Arrays correctly?
    By threwup in forum C Programming
    Replies: 9
    Last Post: 02-10-2011, 07:49 PM
  2. Loop reading input
    By c_newbie in forum C Programming
    Replies: 5
    Last Post: 12-08-2010, 09:09 AM
  3. Reading in a string from an input file?
    By matthayzon89 in forum C Programming
    Replies: 5
    Last Post: 08-31-2010, 02:14 PM
  4. Help with linked list (and reading user input)
    By p1kn1c in forum C Programming
    Replies: 2
    Last Post: 04-05-2006, 12:43 AM
  5. Reading input as an integer
    By n0de in forum C++ Programming
    Replies: 4
    Last Post: 04-11-2002, 06:52 PM