Thread: String problem???

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    9

    Angry String problem???

    Here is my code I cannot figure out how to get my dates that I enter to work properly. The user should enter them in looking like this 20000101 year month day.

    // This is a late charge calculator

    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
    #include <stdlib.h>

    #define year 365
    #define month 30

    void main(void)
    {
    char year1[3],month1[1],day1[1],year2[3],month2[1],day2[1];
    int outstand,inteyear,intemonth,inteday,inteyear2,inte month2,inteday2;
    int balyear,balmonth,balday;
    int peryear,permonth,perday,totaldays,interestowing,to tal;

    printf("\n Enter the Date Today(year,month,date):");
    scanf("%s %s %s",year1,month1,day1);
    inteyear=atoi(year1);
    intemonth=atoi(month1);
    inteday=atoi(day1);

    printf("\n Enter the date it was due(year,month,date):");
    scanf("%s %s %s",year2,month2,day2);
    inteyear2=atoi(year2);
    intemonth2=atoi(month2);
    inteday2=atoi(day2);

    if(inteyear<inteyear2)
    {
    printf("\nPlease ensure that the year and date are correct");
    }
    else
    printf("\n Please Enter the outstanding balance:");
    scanf("%d",outstand);

    balyear=inteyear-inteyear2;
    balmonth=intemonth-intemonth2;
    balday=inteday-inteday;

    totaldays=(balyear*year)+(balmonth*month)+(balday) ;


    interestowing=totaldays*.12; //12 percent interest

    total=outstand+interestowing;

    printf("\nThe total late charge is: %d",interestowing);
    printf("\nThe New Total is: %d", total);
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    If your input is this:

    > The user should enter them in looking like this 20000101 year
    > month day.


    Your problem is this:

    > scanf("%s %s %s",year1,month1,day1);

    Because you're expecting this:

    20000101

    And your code expects this:

    2000 01 01

    See the problem?

    Just read it into a single array, snip the first 4 characters for the year, snip the next two for month, and the last two for day.

    Quzah.

  3. #3
    Unregistered
    Guest
    Try:
    char year1[4],month1[2],day1[2],year2[4],month2[2],day2
    [2];

    Even though the last element of day2 is day2[1] , you should declare it as day2[2].

    Mark

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  2. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  3. Custom String class gives problem with another prog.
    By I BLcK I in forum C++ Programming
    Replies: 1
    Last Post: 12-18-2006, 03:40 AM
  4. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 09:19 AM
  5. Replies: 4
    Last Post: 03-03-2006, 02:11 AM