Thread: screwy output

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    255

    screwy output

    ok im a little new to the C inputing method although i have used the C outputing method of printf a bit i just normally mixed it with cin. but this class is using pure C so thats not allowed anymore.

    my program successfully asked for the name

    then asked for how many tons i want to dispose of in the trash and then it crashes after i give a response is there any reason for this?

    Code:
    main
    {
    
                trash = get_int(msg);
    }
    
    double get_double(char msg[])
    {
        char buf[40];
        printf(msg);
        gets(buf);
        return atof(buf);
    }
    hooch

  2. #2
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    Is that your complete code ?

    Because if it is I can't see how it would possibly compile.

    Show the complete code. Help us help you.
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  3. #3
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Not to give you the privilege of saying that what you wrote made any sense, at all... but even the closest bit of sense I can make out of it has nothing to do with the code you wrote... which makes equally little sense.
    Sent from my iPadŽ

  4. #4
    Registered User
    Join Date
    Nov 2001
    Posts
    255
    well i was just showing where i called it but that was the function that asks the question.

    but i can show you everything

    Code:
    #include <stdlib.h>
    #include <string.h>
    #include <stdio.h>
    #include <conio.h>
    int get_int(char msg[]);
    double get_double(char msg[]);
    void display(char hauler[][40], double tot_trash[], int i);
    
    int main()
    {
        char hauler[40][40];
        char msg[200];
        char end = ' ';
        int trash = 0;
        int l_trash;
        int i = 0;
        double tot_trash[40] = { 0.0 };
        double rate;
        double total = 0.0;
    
        while(end != 'Y')
        {
            printf("What is your name");
            gets(hauler[i]);
    		printf("\n");
            while(trash != 0)
            {
                strcpy(msg,
                       "Enter how much trash you took out(0 to stop)? \n");
    
                trash = get_int(msg);
                tot_trash[i] += trash;
            }
            strcpy(msg, "How Much trash did you dump last month? \n");
    
            l_trash = get_int(msg);
            if(trash < l_trash)
                total = 350.00 * trash;
            else
            {
                strcpy(msg, "Enter the Tonnage Rate? \n");
                rate = get_double(msg);
                total = 350.00 * l_trash;
                total += (350.00 + rate) * (trash - l_trash);
            }
            i++;
            printf("Do you want to enter a new hauler(y or n)? %s\n", end);
            end = getch();
            end = toupper(end);
            display(hauler, tot_trash, i);
        }
        return 0;
    }
    
    int get_int(char msg[])
    {
    	int dummy;
        char buf[40];
        printf(msg);
        gets(buf);
    	dummy = atoi(buf);
        return dummy;
    }
    
    double get_double(char msg[])
    {
        char buf[40];
        printf(msg);
        gets(buf);
        return atof(buf);
    }
    
    void display(char hauler[][40], double tot_trash[], int i)
    {
        for(int j = 0; j < i; j++)
        {
            printf("Hauler: %s spent %d\n", hauler[j], tot_trash[j]);
        }
    }
    hooch

  5. #5
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    ok, although I don't quite know what you mean by "crash", I could find a reason as to why you wouldn't get any output. There is no place anywhere in your code in which you increment i, so when you get to display, i is still 0, so display skips the loop.

    My bad. I should really be more attentive.
    Last edited by Happy_Reaper; 01-26-2006 at 09:59 PM.
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  6. #6
    Registered User
    Join Date
    Nov 2001
    Posts
    255
    i increments right underneeath the if else

    and by crash i mean big error debug message you know that one dont send debug whatever?

    but it crashes after i enter the second question it asks how many tons of trash i input say 6 and it then it bombs out as if it it cant even make it through question two for some reason?
    hooch

  7. #7
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    What's the error message ?
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  8. #8
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    This is for a class?

    I don't know a teacher that would accept this program. You're using non-standard libraries, you're using a C99 for loop (which by the way, if that's compiling, then you're compiling it as a .cpp not a .c as it should be) and your functions are a mess. You shouldn't have all those printf() and gets() functions inside of your functions.

    If you're actually still interested in knowing where your program is crashing, it's on this line:

    Code:
    printf("Do you want to enter a new hauler(y or n)? %s\n", end);
    should be...

    Code:
    printf("Do you want to enter a new hauler(y or n)? %s\n", &end);
    Sent from my iPadŽ

  9. #9
    Registered User
    Join Date
    Nov 2001
    Posts
    255
    well for school this is how its set up:

    we have C programming 1 which is actually C++.net so we have that compiler and C programming 2 is actually old school C using the same compiler which lacks a .c file at least we cant find one. and from what i gather he finds dealing with old C and .net a headache and hes probably still kind of unfamilar with this compiler cause we just started C++.net last year so its a new venture and are debating whether to stick with it or not. so yes i know its not .c file but eh it works.

    out of curiousity why does it need &end vs just end?

    EDIT:

    also this is a tech school so it kind of flows with whatever is local business standard and apparently a bunch of places are windows.net programming freaks so we are also changing curriculm around and whatnot. this is probably the first time theyve taught C under .net environment so any weirdness is forgiveable i suppose. since not alot of people are in programming cirrculm this class is only taught once a year. and right now i think they said its mainly just to get you used to a different dialect as said msot places here are apparently windows.net freaks but who knows they are still wrestling with .net vs standard C/C++.
    Last edited by ssjnamek; 01-26-2006 at 10:13 PM.
    hooch

  10. #10
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Code:
    printf("Do you want to enter a new hauler(y or n)? %c\n", end);
    end is a char. It should be treated as such, and not as a string, even if passing it by pointer will appear to help it invokes undefined behavior.

  11. #11
    Registered User
    Join Date
    Nov 2001
    Posts
    255
    ok how come when my program gets to ask if i want a new hauler any touches of the keyboard count as enter????

    sorry still new to C inputting.
    hooch

  12. #12
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    No, I just said it doesn't work. If you gave that to someone as C source code, they throw it back at you and say it doesn't compile. Before C99 you didn't declare variables in a for loops condition statement.

    Code:
     /* This... */
    for(int x = 0; x < 10; x++) {}
    
    /* Should be this */
    int x;
    for (x = 0; x < 10; x++) {}
    Regardless of whether or not it works for you, as a general programming rule, you should write programs as standard as possible. What works for you might not work for the next guy or the next compiler.

    The reason your need the & symbol is because the %s is looking for a string, or in other words, the address of the beginning of the string. Since your variable is a character (value) you must pass the address of that variable to the function. Another more practical method would be:

    Code:
    printf("Do you want to enter a new hauler(y or n)? %c\n", end);
    Sent from my iPadŽ

  13. #13
    Registered User
    Join Date
    Nov 2001
    Posts
    255
    ah well that explains why i always had to change that when i opened stuff in a Dev compiler never could figure out why that didnt work before now.

    why is my program skipping over the while(trash != 0)

    ive been staring at that wondering why its skipping over

    i have trash set to 0 so it should go in there until it gets set to 0(when they quit) makes sense to me?
    hooch

  14. #14
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Think about what you're saying...

    You have trash equal to 0...

    You have a statement that runs while trash doesn't equal 0...

    It skips it because...
    Sent from my iPadŽ

  15. #15
    Registered User
    Join Date
    Nov 2001
    Posts
    255
    lol ive got to stop making such goofy mistakes...

    now how come anytime it asks if i want to enter a new hauler it screws up with the input??? any key i press and it just goes forward ignoring that line and doesnt even require me pressing enter?
    Last edited by ssjnamek; 01-26-2006 at 11:23 PM.
    hooch

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help for my output array
    By qwertysingh in forum C Programming
    Replies: 1
    Last Post: 02-17-2009, 03:08 PM
  2. execl()/fork() output
    By tadams in forum C Programming
    Replies: 19
    Last Post: 02-04-2009, 03:29 PM
  3. Replies: 4
    Last Post: 11-30-2005, 04:44 PM
  4. Formatting output into even columns?
    By Uncle Rico in forum C Programming
    Replies: 2
    Last Post: 08-16-2005, 05:10 PM
  5. Output problems with structures
    By Gkitty in forum C Programming
    Replies: 1
    Last Post: 12-16-2002, 05:27 AM