stuck and getting tired of being so...

This is a discussion on stuck and getting tired of being so... within the C Programming forums, part of the General Programming Boards category; Hi there, I get stuck when I think I understand my classes every time and it never fails, I am ...

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    18

    stuck and getting tired of being so...

    Hi there, I get stuck when I think I understand my classes every time and it never fails, I am getting REAL tired of it. Maybe someone can fill in my teachers BLANKS!

    This code isn't working. All I get when I run it is 0's

    Code:
    #include <stdio.h>
    #include <string.h>
    /**************/
    /*Brandi Savoy*/
    /*asgn06.c    */
    /*Monday Night*/
    /**************/
    
    #define PROGRAMMER "Brandi Savoy"
    void name();
    void eoj();
    int readnum(int x);
    int square(int x);
    int cube(int x);
    int add(int x, int y);
    int subt(int x, int y);
    
    void main()
    {
     int num1, num2, sqrt1, sqrt2, cub1, cub2, tot, diff; 
     readnum(num1);
     readnum(num2);
     sqrt1 = square(num1);
     sqrt2 = square(num2);
     cub1 = cube(num1);
     cub2 = cube(num2);
     tot = add(num1, num2);
     diff = subt(num1, num2);
    
     name();
     printf("\nThe square of num1 is:\t\t\t\t\t %d\n\n", sqrt1);
     printf("The square of num2 is:\t\t\t\t\t %d\n\n", sqrt2);
     printf("The cube of num1 is:\t\t\t\t\t %d\n\n", cub1);
     printf("The cube of num2 is:\t\t\t\t\t %d\n\n", cub2);
     printf("The total of num1 and num2 is:\t\t\t\t %d\n\n", tot);
     printf("The difference between num1 and num2 is:\t\t %d\n\n", diff);
     printf("Press ENTER to continue");
     fflush(stdin);
     getchar();
    }
    
    int readnum(int x)
    {
     int rn;
     name();
     printf("key in an integer:");
     fflush(stdin);
     scanf("%d", &rn);
     return rn;
    }
    
    int square(int x)
    {
     return x * x; 
    }
    
    int cube(int x)
    {
     return x * x * x;
    }
    
    int add(int x, int y)
    {
     int ad = x + y; 
     return ad; 
    }
    
    int subt(int x, int y)
    {
     int sb = x - y;
     return sb; 
    }
    
    void name()
    {
     system("clear");
     printf("%s\n\n\n", PROGRAMMER);
    }
    
    void eoj()
    {
     name();
     printf("EXECUTION COMPLETED... Press ENTER to contiune.\n");
     getchar();
    }
    Thanks in advance

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,699
    >> readnum(num1);

    Try:

    num1 = readnum(num1);

    The function returns the result, but you're just not capturing it.
    Code:
    bool fun(bool value)
    {
        return std::pow(std::exp(1), std::complex<float>(0, 1) 
        * std::complex<float>(std::atan(1)*(1 << (value + 2))))
        .real() > 0;
    }

  3. #3
    Registered User
    Join Date
    Mar 2004
    Posts
    18

    oh man, duh!

    your awesome! Thanks for clearing out my brain fart!!!

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >I am getting REAL tired of it
    Get used to it if you want to program in any serious capacity.

    >void name();
    >void eoj();
    There is a subtle distinction between no arguments and unspecified arguments. You should use void to specify no arguments:
    Code:
    void name(void);
    void eoj(void);
    >void main()
    No, main always returns int:
    Code:
    int main(void)
    >int num1, num2, sqrt1, sqrt2, cub1, cub2, tot, diff;
    Here's a trick. If your variable count exceeds seven, your function is too complex.

    >fflush(stdin);
    fflush is only defined for output streams. This may work for you, but it is all kinds of wrong.

    >getchar();
    If you correctly changed main, you also need to return a value:
    Code:
    getchar();
    return 0;
    >scanf("%d", &rn);
    It's a good idea to check all interactive input for success before assuming it is correct:
    Code:
    if ( scanf ( "%d", &rn ) != 1 )
      return errorVal; /* You define errorVal */
    >int ad = x + y;
    >return ad;
    Why not just:
    Code:
    return x + y;
    >system("clear");
    If you're not on a Windows system then I don't see how you can expect fflush(stdin) to work. I wasn't aware it did elsewhere.
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    Mar 2004
    Posts
    18

    Re

    There is a lot of stuff in my class thats I just don't know why we learn that way. We lose marks if we don't code according to how we learned C. All this is good advice I will keep in mind, unfortunatly I have to keep the code (save for the ad function, return x + y;, I thought I got that one..) basically as it is.

    We work in a unix system and I have had to put in fflush(stdin) a few times because it was giving me trouble without it. Dunno much about it in other systems but it seems to work in unix.

    =)

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >We lose marks if we don't code according to how we learned C.
    Your teacher is an idiot if he/she teaches bad C and then requires you to do so as well.
    My best code is written with the delete key.

  7. #7
    Registered User
    Join Date
    Dec 2003
    Posts
    53
    By the way, in my humble opinion, this function is misleading, and could be modified in one of two ways:

    Code:
    int readnum(int x) //Why is it taking in the x value here? does nothing with it
    {
       int rn;
       name();
       printf("key in an integer:");
       fflush(stdin);
       scanf("%d", &rn);
       return rn;
    }

    In this case, I did nothing more than replace the int x with void as int x was never used anywhere in the function to begin with.

    Note, when calling this function, you should use it like this:
    val1 = readnum();
    Code:
    int readnum(void) // Doesn't need to receive anything from main
    {
       int rn;
       name();
       printf("key in an integer:");
       fflush(stdin);
       scanf("%d", &rn);
       return rn;
    }

    If you haven't learned pointers, ignore this.
    It is also possible to return values to the main function by using pointers. This means that your function can return 1 or more values to different variables at the same time, yet lets you use the return to return a status report.
    Code:
    void readnum(int *x) //Modified to use pointers
    {
       name();
       printf("key in an integer:");
       fflush(stdin);
       scanf("%d", x);
    }

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,488
    > We work in a unix system and I have had to put in fflush(stdin)
    Which one?
    I don't think I've ever come across a unix system where this did anything other than either nothing or crash.
    fflush() is for OUTPUT streams, and UPDATE streams following a write. stdin is an INPUT stream, and therefore can never be a valid stream for a fflush()

    The need for attempting to flush the input stream pretty much goes away when you wean yourself off scanf(), and just stick to using fgets() as the sole method of primary input. Once the input is in a buffer, you can do whatever you want with it without worrying about messing up the input stream (as scanf does so readily).
    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.

  9. #9
    Registered User
    Join Date
    Mar 2004
    Posts
    18

    Re

    I don't know what system it is unless there is UNIX command you know that I can type in and it will tell me. it says on login "Machine Configuration: (IBM RS6000/S70 AIX 4.3.3) Does that mean something?

    The only thing I am aware of fflush doign is it doesn't skip my getchars when I want the screen to hold before going onto the next screen, I had a few screens overpassed a fewtimse without fflush but other than that we are just told to put it in there before every scanf.

    Quote Originally Posted by Salem
    > We work in a unix system and I have had to put in fflush(stdin)
    Which one?
    I don't think I've ever come across a unix system where this did anything other than either nothing or crash.
    fflush() is for OUTPUT streams, and UPDATE streams following a write. stdin is an INPUT stream, and therefore can never be a valid stream for a fflush()

    The need for attempting to flush the input stream pretty much goes away when you wean yourself off scanf(), and just stick to using fgets() as the sole method of primary input. Once the input is in a buffer, you can do whatever you want with it without worrying about messing up the input stream (as scanf does so readily).

  10. #10
    Registered User
    Join Date
    Mar 2004
    Posts
    18

    Re

    LOL you know... your not the first person to say so... I've been reluctant to post for help because of this fact. I can't wait until Ican do my own C and get a proper understanding of it.

    Quote Originally Posted by Prelude
    >We lose marks if we don't code according to how we learned C.
    Your teacher is an idiot if he/she teaches bad C and then requires you to do so as well.

  11. #11
    Registered User
    Join Date
    Dec 2003
    Posts
    53
    Quote Originally Posted by escarrina
    The only thing I am aware of fflush doign is it doesn't skip my getchars when I want the screen to hold before going onto the next screen, I had a few screens overpassed a fewtimse without fflush but other than that we are just told to put it in there before every scanf.
    What happening here is the work of the scanf function...
    In my experience with it(and alot of other more experienced programmers around here), scanf has a nasty tendency of leaving things in the input stream AKA stdin that can then screw up your other scanf()s, as well as getchar and getch.

    The generally accepted way of cleaning the input buffer around here is actually mentioned in the faq.

    essentially, replace the line
    fflush(stdin);
    with
    char temp; // Remeber to put this where you initialize your variables...
    while((temp = getchar()) != EOF ||temp != '\n');

    this will clear out your input stream, and is as Prelude likes to say, "not undefined behavior"

    Although I confess to using fflush(stdin); myslef sometimes when I'm lazy

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