Some help plz.

This is a discussion on Some help plz. within the C Programming forums, part of the General Programming Boards category; I'm coding a c program that will take 3 integers and print them in the original format entered then in ...

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    2

    Some help plz.

    I'm coding a c program that will take 3 integers and print them in the original format entered then in reverse using four functions. It works to the extent that the numbers aren't printing correctly. Help would be greatly appreciated! Here's my code:
    Code:
    #include <stdio.h>
    
    int getData (int* a, int* b, int* c);
    int printOriginal (int* a, int* b, int* c);
    int printReverse (int* a, int* b, int* c);
    
    int getData (int* a, int* b, int* c) {
        
        printf("Enter three intergers:\n");
        scanf("%d %d %d", &a, &b, &c);
        
        return(&a, &b, &c);
        }
    
    int printOriginal (int* a, int* b, int* c) {
        
        printf("%d %d %d\n", a, b, c);
        
        return(&a, &b, &c);
        }
        
    int printReverse (int* a, int* b, int* c) {
        
        printf("%d %d %d\n", c, b, a);
        
        return(&a, &b, &c);
        }
    
    int main (void) {
        int a;
        int b;
        int c;
        
        getData (&a, &b, &c);
        printOriginal (&a, &b, &c);
        printReverse (&a, &b, &c);
    
        system("pause");
        return 0;
        }
    Thanks!
    -Max

  2. #2
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    You have some knowledge misconceptions here. Read through Lesson 6 Pointers and Lesson 4 Functions. After doing that you will be able to see what you are doing wrong.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    int getData (int* a, int* b, int* c) {
        
        printf("Enter three intergers:\n");
        scanf("%d %d %d", &a, &b, &c);
        
        return(&a, &b, &c);
        }
    Turn on your compiler's warnings already. It should be screaming at you.

    You already have pointers, so why are you using & in your scanf call?
    You can't return multiple things.
    Even if you could, you are trying to return the address of pointers, not the value of it (your return type is int).
    Why do you even have this function? Just call scanf as-is.

    That's as far as I got. Fix that before you start on the rest.


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

  4. #4
    Registered User
    Join Date
    Sep 2011
    Posts
    2
    I'm suppose to have four functions: main, one to read the data, one to print the order read, and one to print them in reverse. That's why scanf has it's own function. I could easily code this program in one function, but I'm having trouble using multiple functions to accomplish something so simple in one function. Thanks for the quick responses.

  5. #5
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Quote Originally Posted by wstrahan View Post
    I'm suppose to have four functions: main, one to read the data, one to print the order read, and one to print them in reverse. That's why scanf has it's own function. I could easily code this program in one function, but I'm having trouble using multiple functions to accomplish something so simple in one function. Thanks for the quick responses.
    Because this assigment is suppose to teach you about pointers. Read through the links I posted and look at quzah's comments on things wrong. Then you should be able to finish this task without a problem.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by wstrahan View Post
    I'm suppose to have four functions: main, one to read the data, one to print the order read, and one to print them in reverse. That's why scanf has it's own function. I could easily code this program in one function, but I'm having trouble using multiple functions to accomplish something so simple in one function. Thanks for the quick responses.
    Even stranger... you are trying to return multiple values (which C cannot do) but your calls to the functions ignore the returns...

    You are feeding pointers in the top of your functions... work with them.

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