Some Help Please

This is a discussion on Some Help Please within the C Programming forums, part of the General Programming Boards category; I'm having a problem with some homework and was wondering if anyone can point me in the right direction... the ...

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    46

    Unhappy Some Help Please

    I'm having a problem with some homework and was wondering if anyone can point me in the right direction... the purpose of this project is to write a program that will store values for a "cd" memory database test the values so see if they will fit on the cd and when all values are entered print out summery of the entered values sorted by song #. I haven't been able to implement everything yet but am getting 2 compile time errors I can't seem to fix. Because I basically don't know what they mean. Thanks to anyone for their help in advance.

    //Michael Handlon
    //Project 3

    #include <stdio.h>
    #define SIZE 10

    int main(void){

    int mysonlist[SIZE];
    int myminlist[SIZE];
    int myseclist[SIZE];
    int total_left = 2700;
    int tt = 0;
    int min_left;
    int seconds_left;
    int min;
    int sec;
    int son;
    int sum_min = 0;
    int sum_sec = 0;
    char ch;

    while (ch != 'q'){

    printf("\nEnter the song # you would like to add (or q to quit):");
    scanf("%d",&son);
    printf("\nHow many minutes long is song %d?",son);
    scanf("%d",&min);
    printf("\nHow many seconds long is song %d?",son);
    scanf("%d",&sec);
    total_left = (total_left - (min*60) - sec);
    tt = (total_time + (min*60) + sec);
    min_left = (time_left/60);
    sec_left =(total_left - (min_left*60));

    if (time_left >= 0){
    printf("Song #%d will fit on the cd you may include it.");
    mysonlist[son] = son;
    myminlist[son] = min;
    myseclist[son] = sec;
    }

    else if (time_left <= 0){
    printf("Song #%d will not fit onto the cd you can't include it.");
    }
    } // end while

    if(ch == 'q'){

    printf("\n");
    printf("Song Song Song Total Time\n");
    printf("Number Minutes Seconds Minutes Seconds\n");
    printf("------ ------- ------- ------- -------\n");
    for (i=0;i<10;i++){
    sum_min = sum_min + myminlist[i];
    sum_sec = sum_sec + myseclist[i];
    printf("%d %d %d %d %d \n, mysonlist[i], myminlist[i], myseclist[i], sum_min, sum_sec); //possible real stat of unterminated constant error on this line
    }

    printf("\n");
    printf("\nThere are %d minutes and %d seconds of tape left.\n",sum_min,sum_sec);
    printf("\n"); // unterminated string or charactor constant here
    }
    return 0;
    }

  2. #2
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,825
    You say this line gives an error? You're missing smth here...
    Code:
    printf("%d %d %d %d %d \n, mysonlist[i], myminlist[i], myseclist[i], sum_min, sum_sec);
    Count your quotes...

    Your second error sounds like it might be a result of the first one, too. Change it and let us know.

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    46
    You were right about that.... thier is something wrong now with my for loop it's infinate and I don't see why and I just want a push in the right direction on how to sort the end list without having a bunch of white space. I fixed the multiple little errors in the code so it will compile now and is more workable.

    //Michael Handlon
    //Project 3

    #include <stdio.h>
    #define SIZE 11

    int main(void){

    int mysonlist[SIZE];
    int myminlist[SIZE];
    int myseclist[SIZE];
    int total_left = 2700;
    int tt = 0;
    int min_left;
    int sec_left;
    int seconds_left;
    int min;
    int sec;
    int son;
    int sum_min = 0;
    int sum_sec = 0;
    char ch;

    while (ch != 'q'){

    printf("\nEnter the song # you would like to add (or q to quit):");
    scanf("%d",&son);
    printf("\nHow many minutes long is song %d?",son);
    scanf("%d",&min);
    printf("\nHow many seconds long is song %d?",son);
    scanf("%d",&sec);
    total_left = (total_left - (min*60) - sec);
    tt = (tt + (min*60) + sec);
    min_left = (total_left/60);
    sec_left =(total_left - (min_left*60));

    if (total_left >= 0){
    printf("Song #%d will fit on the cd you may include it.",son);
    mysonlist[son] = son;
    myminlist[son] = min;
    myseclist[son] = sec;
    }

    else if (total_left <= 0){
    printf("Song #%d will not fit onto the cd you can't include it.");
    }
    } // end while

    if(ch == 'q'){

    printf("\n");
    printf("Song Song Song Total Time\n");
    printf("Number Minutes Seconds Minutes Seconds\n");
    printf("------ ------- ------- ------- -------\n");
    for (int i=0;i<10;i++){
    sum_min = sum_min + myminlist[i];
    sum_sec = sum_sec + myseclist[i];
    printf("%d %d %d %d %d \n", mysonlist[i], myminlist[i], myseclist[i], sum_min, sum_sec);
    }
    printf("\n");
    printf("\nThere are %d minutes and %d seconds of tape left.\n",sum_min,sum_sec);
    printf("\n");
    }
    return 0;
    }







  4. #4
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,825
    I'm looking at the for loop thing... This'll give you a problem, tho:
    Code:
    printf("Song #%d will not fit onto the cd you can't include it.");
    Think about what %d is going to be...

    Also, your if(ch == 'q') is unecessary - for the code to get there in the first place, it has to be true.

  5. #5
    Registered User
    Join Date
    Sep 2001
    Posts
    46
    I dumped the needless code..... I assumed the first time it was the for loop giving me the problem but it's looping the if loop it seems.

  6. #6
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,825
    Oh, duh!

    Think about this one...

    When is ch ever changed? It's not initalized, and you include no place where it could be changed. Then, when you ask for a song num or q to quit, you put the answer in son, which is an int... Kinda see where I'm going? Play with that...

  7. #7
    Registered User
    Join Date
    Sep 2001
    Posts
    46
    OMG... WTF I'm an idiot.

  8. #8
    Registered User
    Join Date
    Sep 2001
    Posts
    46
    **** me but this still isn't working....

    //Michael Handlon
    //Project 3

    #include <stdio.h>
    #define SIZE 11

    int main(void){

    int mysonlist[SIZE];
    int myminlist[SIZE];
    int myseclist[SIZE];
    int total_left = 2700;
    int tt = 0;
    int min;
    int sec;
    int son;
    int sum_min = 0;
    int sum_sec = 0;
    char ch;

    printf("\nWelcome to Wimamp+");

    while (ch != 'y'){
    printf("\nWould you like to add a song to your playlist ('y' or 'n')?");
    scanf("%c",&ch);
    printf("\nEnter the song # you would like to add:");
    scanf("%d",&son);
    printf("\nHow many minutes long is song %d?",son);
    scanf("%d",&min);
    printf("\nHow many seconds long is song %d?",son);
    scanf("%d",&sec);
    total_left = (total_left - (min*60) - sec);
    tt = (tt + (min*60) + sec);

    if (total_left >= 0){
    printf("Song #%d will fit on the cd you may include it.\n",son);
    mysonlist[son] = son;
    myminlist[son] = min;
    myseclist[son] = sec;
    }

    if (total_left <= 0){
    printf("Song #%d will not fit onto the cd you can't include it.\n",son);
    }

    } // end while

    printf("\n");
    printf("Song Song Song Total Time\n");
    printf("Number Minutes Seconds Minutes Seconds\n");
    printf("------ ------- ------- ------- -------\n");

    for (int i=0;i<10;i++){
    sum_min = sum_min + myminlist[i];
    sum_sec = sum_sec + myseclist[i];
    printf("%d %d %d %d %d \n", mysonlist[i], myminlist[i], myseclist[i], sum_min, sum_sec);
    } // end for

    printf("\n");
    printf("\nThere are %d minutes and %d seconds of tape left.\n",sum_min,sum_sec);
    printf("\n");


    return 0;
    }

  9. #9
    Registered User
    Join Date
    Sep 2001
    Posts
    46
    Ok... I furgot to explain the current problem that's not being fixed... when the while (ch != 'y'){ loop goes through it seems to run fine but makes no mind and just skips the scanf on the 2,3,4.. interations of the loop. And if I enter no on the 1st time it dosn't take mind of it. Whats the proper way to write this error checking in c?

  10. #10
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,668
    Yet another newbie comes unglued due to the odd behaviour of the scanf function.

    scanf takes only the MINIMUM number of characters from the input stream. This means that if you type in an integer (123), and press return, and use scanf("%d") to read that integer, it will leave the return character on the input stream.

    This usually means that if you try and use scanf to read a specific character (say the answer to a yes/no question), you'll invariably end up with a return character left behind by some previous scanf call, and not the y/n you expected.

    Put this line of code in here....

    Code:
        while ( getchar() != '\n' );   // remove all unwanted trailing characters
    } // end while
    > printf("\nWould you like to add a song to your playlist ('y' or 'n')?");
    You're still forced to enter data even if you say no.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  11. #11
    Registered User
    Join Date
    Aug 2001
    Posts
    247
    Hi,
    Govtcheez told you why it won't work, CHAR CH needs to be initialized before the first loop....To tidy up you could use library file #include <conio.h> and use getch(); instead of scanf for character input and #include <ctype.h> for to lower function.
    Also you cannot use assignment op = in if comparison, use is equal to (==), you are using not equal to y, there for when y was selected the loop ended next comparison test. There is now no need for the declaration of char ch; get rid.

    In the changed code below I have implemented the above, and it compiles ok.
    Code:
    printf("\nWelcome to Wimamp+");
    printf("\nWould you like to add a song to your playlist ('y' or 'n')?");
    
    while (tolower(getch())== 'y'){
    
    printf("\nEnter the song # you would like to add:");
    scanf("%d",&son);
    printf("\nHow many minutes long is song %d?",son);
    scanf("%d",&min);
    printf("\nHow many seconds long is song %d?",son);
    scanf("%d",&sec);
    total_left = (total_left - (min*60) - sec);
    tt = (tt + (min*60) + sec);
    
    if (total_left >= 0){
    printf("Song #%d will fit on the cd you may include it.\n",son);
    mysonlist[son] = son;
    myminlist[son] = min;
    myseclist[son] = sec;
    }
    
    if (total_left <= 0){
    printf("Song #%d will not fit onto the cd you can't include it.\n",son);
    }
    /*prompt user for next entry*/
    printf("\nWould you like to add a song to your playlist ('y' or 'n')?");
    } // end while
    Last edited by bigtamscot; 09-24-2001 at 02:01 PM.
    hoping to be certified (programming in c)
    here's the news - I'm officially certified.

Popular pages Recent additions subscribe to a feed

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