strlen problem

This is a discussion on strlen problem within the C Programming forums, part of the General Programming Boards category; Hi guys the other day i posted a question regarding inputing data into a string. After much research my code ...

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    8

    strlen problem

    Hi guys the other day i posted a question regarding inputing data into a string. After much research my code has changed lots and i have got to this point

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int input;
    int q =0;
    int p;
    int d=0;
    int main(){
      char sortingarray[5];
      printf("Please enter a 4 digit number\n");
      fgets ( sortingarray, 5, stdin );
      d = strlen(sortingarray);
      printf("%d",d);
      
      
      if (d == 4){       
                  printf("\n There are %c thousands in your number\n", sortingarray[0]);
                  printf("\n There are %c hundreds in your number\n", sortingarray[1]);
                  printf("\n There are %c tens in your number\n", sortingarray[2]); 
                  printf("\n There are %c ones in your number\n\n", sortingarray[3]); 
        }  
     
        else if (d==3){       
               printf("\n There are 0 thousands in your number\n");
               printf("\n There are %c hundreds in your number\n", sortingarray[0]);
               printf("\n There are %c tens in your number\n", sortingarray[1]); 
               printf("\n There are %c ones in your number\n\n", sortingarray[2]); 
    
        }   
        else if (d==2){       
                printf("\n There are 0 thousands in your number\n");
                printf("\n There are 0 hundreds in your number\n");
                printf("\n There are %c tens in your number\n", sortingarray[0]); 
                printf("\n There are %c ones in your number\n\n", sortingarray[1]); 
    
        }   
        else if(d==1){       
                  printf("\n There are 0 thousands in your number\n");
                  printf("\n There are 0 hundreds in your number\n");
                  printf("\n There are 0 tens in your number\n"); 
                  printf("\n There are %c ones in your number\n\n", sortingarray[0]); 
    
        }   
    
        system("PAUSE"); 
        return 0;
        
    }
    I am having problems with the strlen part of it. What the program needs to do is tell the user how many 1000's, 100's, 10's and 1's that have been entered with a maximum number size of 9999. If 123 is entered the program must say that the is 0 1000's, 1 100, 2, 10's and 3 1's. To be able to do this i have created a series of if and else if statements. What i am having with is when you enter a 4 digit number the program works as it sees that there have been 4 numbers entered. Where it stops working properly is when there have been 3 2 and 1 numbers entered. They give answers but they are 1 number out. So if 3 is entered strlen says that 4 has been entered. When 2 have been entered strlen says there has been 3 entered and so on. What is causing this. It is really confusing me.

    Thanks in advance

    Sam

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    fgets keeps the \n in your string. So if you want 4 digits + newline + nul, you need a bigger array. Either that or you need to take the newline out of the array before you check its length.


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

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    What I don't uderstand is why you aren't doing this mathematically... using the modulous operator and dividing is going to work a lot better than trying to dissect a string.

  4. #4
    Registered User
    Join Date
    May 2011
    Posts
    8
    because i have no idea how you would do it mathematically. Also we haven't gone that far into programming. My course is just based around simple programming for mechanical engineering. How would you do it mathematically?

    Cheers
    Sam

  5. #5
    Registered User
    Join Date
    May 2011
    Posts
    8
    because i have no idea how you would do it mathematically. Also we haven't gone that far into programming. My course is just based around simple programming for mechanical engineering. How would you do it mathematically?

    Cheers
    Sam

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You're in a mechanical engineering course and you're not familiar with scientific or engineering notation?

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Or how to divide by 10.


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

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Ok if you're in an engineering course, you must be familiar with integer math... (or at least you had better be)
    So what is 123 mod 10?
    Next what is 123 div 10?

    Does this not suggest a process to you?

    And you can take my word for it... Mechanical Engineering is NOT simple programming...

  9. #9
    Registered User
    Join Date
    May 2011
    Posts
    8
    i answered that wrong. i know how you can do it mathematically but writing a program that can do that i have not done yet. we have covered strings, arrays, functions and that is about it so far. Commontater i know that it is not simple as i have 2 more courses after this one on computer programming in both c and matlab. So i am guessing it will get harder. But i have only been doing this course for 5 weeks and some of that was a holiday as i live in Christchurch New Zealand and the earthquake we had stuffed my year up so we are playing catch up.

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    1) There's a first time for everything...
    2) So what says you can't read ahead in your textbooks...
    3) I appreciate the problems you had "down under" and hope you and yours are ok...

    BUT... if you can't figure out the really easy stuff (I can do this in less than 10 lines) on your own... you're screwed when the hard stuff comes.

  11. #11
    Registered User
    Join Date
    May 2011
    Posts
    8
    when i get some time i will see if i can do it more efficiently. At the moment i was just using what i know so i can get it done and more onto my next assignment. Thanks for you help. I will see if i can write a program using your method.

    Thanks
    Sam

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strlen intermittent problem
    By JM1082 in forum C Programming
    Replies: 5
    Last Post: 05-10-2011, 10:27 AM
  2. Problem using strcmp and strlen in a Linked List
    By matrixx333 in forum C Programming
    Replies: 4
    Last Post: 11-23-2009, 03:13 AM
  3. Problem: Bubblesort - "strlen" in the function...
    By Petike in forum C Programming
    Replies: 15
    Last Post: 01-24-2008, 08:20 PM
  4. Problem with strlen()
    By ilmarculin in forum C Programming
    Replies: 2
    Last Post: 02-17-2005, 08:10 AM
  5. strlen problem
    By niroopan in forum C++ Programming
    Replies: 2
    Last Post: 12-10-2002, 04:46 PM

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