Thread: Moron Newbie needs help with errors please!

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    13

    Moron Newbie needs help with errors please!

    It's homework(Worth way too much of my grade) for a class (due lunch monday yayitsallmyfaultforfailing), and I'm getting errors that I can't figure out at all. Googling the names of the errors either gets me a completely useless microsoft error page, or a random forum post somewhere on the net that is so complicated I can't understand anything. Please, (benevolent internetizens) tell me what I'm doing wrong, and phrase it nice and simply. The assignment is all about pointers, and I'm supposed to shuttle numbers and letters around to make a purchasing thingy and all those uh....declared mini-program thingies at the beginning come from the assignment and I have to make them work.

    Errors:

    (1) : warning C4067: unexpected tokens following preprocessor directive - expected a newline
    (19) : error C2100: illegal indirection
    (19) : error C2664: 'GetCost' : cannot convert parameter 2 from 'double' to 'double *'
    (32) : error C2447: '{' : missing function header (old-style formal list?)

    Program:

    Code:
    #include <stdio.h>;
    
    void Menu(char *purchase);
    void GetCost(char purchase, double *item_cost);
    void MoneyMenu(double *deposits, double item_cost);
    int CheckMoney(double deposits, double item_cost);
    void GetMoney(double *deposits, double item_cost, char purchase);
    void GetChange(double *deposits, double item_cost);
    void Quit(char *again);
    
    int main()
    {	
    	char purchase;
    	double item_cost;
    	printf("Welcome to the App Store\n");
    	Menu(&purchase);
    	printf("%c", purchase);
    	printf("%c\n", purchase);
    	GetCost(purchase, *item_cost);
    	printf("This item costs $%d", item_cost);
    	return 0;
    }
    
    void Menu(char *purchase)
    {
    	printf("Products:\nG-GPS navigation $4.99\nE-Flashlight $0.99\nE-EBay autobidder $12.99\nR-Resturaunt locator $2.99\nP-Game package $9.99\n");
    	scanf("%c", &purchase);
    	printf("%c", purchase);
    }
    
    void GetCost(char purchase, double *item_cost);
    {
    	if (purchase==g)
    		*item_cost='4.99';
    	else
    		*item_cost='5.99';
    }
    Also, the class is for programing in C, but the compiler is C++ 2005 express. I don't know if that matters or not, or if this is in the wrong forum. Sorry if I posted this in the wrong place.

  2. #2
    Programming King Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Middle of NoWhere
    Posts
    320
    Well, you've declared double item_cost but you are trying to use it like a pointer. double is a datatype that doesn't mean that by doing this, you are able to declare a pointer. If you want to create a pointer of double datatype, you'll must have to do;
    Code:
    double * item_cost=null;
    I don't have enough knowledge about the compilers but according to me it should not create any trouble...
    I don't care if someone doesn't like me, i was not put on earth to entertain everyone.

    No King, no Queen, I am the ACE of battle.

  3. #3
    Registered User
    Join Date
    Mar 2010
    Posts
    583
    Quote Originally Posted by inverse9 View Post
    Errors:

    (1) : warning C4067: unexpected tokens following preprocessor directive - expected a newline
    You shouldn't put a semicolon at the end of preprecessor directives.

    Quote Originally Posted by inverse9 View Post
    (19) : error C2100: illegal indirection
    (19) : error C2664: 'GetCost' : cannot convert parameter 2 from 'double' to 'double *'
    This one is self explanatory if you're happy with pointers -- since you're learning about them I guess you're not

    GetCost takes a char and a pointer-to-double. You need to use the & operator to take the address (i.e. get the pointer) to item_cost.

    Like so
    Code:
    GetCost(purchase, &item_cost);
    The * you had there before does the opposite of what you want -- * dereferences pointers - i.e. returns the basic type. Here's the same sentence in code: (thought you should probably look this up properly - it's important )

    Code:
    int myInt = 5;
    int *myIntPtr = &myInt;
    myint = *myIntPtr;
    Quote Originally Posted by inverse9 View Post
    (32) : error C2447: '{' : missing function header (old-style formal list?)
    You have another extraneous semicolon after the declaration of GetCost(). This means that the compiler interprets it as a function prototype (fine) but then the braced-block following it is illegal.

  4. #4
    Registered User
    Join Date
    Mar 2011
    Posts
    13
    Ok, I actually got the program to compile and run, but apparently my pointers aren't actually sending stuff right. My prinf stuff in the main isn't working, and I don't think the if else thing is actually giving making item_cost it's number.

    Code:
    #include <stdio.h>
    
    void Menu(char *purchase);
    void GetCost(char purchase, double *item_cost);
    void MoneyMenu(double *deposits, double item_cost);
    int CheckMoney(double deposits, double item_cost);
    void GetMoney(double *deposits, double item_cost, char purchase);
    void GetChange(double *deposits, double item_cost);
    void Quit(char *again);
    
    int main()
    {	
    	char purchase;
    	double item_cost;
    	printf("Welcome to the App Store\n");
    	Menu(&purchase);
    	printf("%c\n", purchase); //This prints a character that looks weird. I can't describe it easily.
    	GetCost(purchase, &item_cost);
    	printf("This item costs $%f", item_cost); //This prints $892221753.000000
    	return 0;
    }
    
    void Menu(char *purchase)
    {
    	printf("Products:\nG-GPS navigation $4.99\nE-Flashlight $0.99\nE-EBay autobidder $12.99\n");
    	printf("R-Resturaunt locator $2.99\nP-Game package $9.99\nPlease enter a selection:");
    	scanf("%c", &purchase);
    	printf("%c", purchase);//This prints the letter I enter.
    }
    
    void GetCost(char purchase, double *item_cost)
    {
    	if (purchase=='g')
    		*item_cost='4.99';
    	else
    		*item_cost='5.99';
    	printf("%f", item_cost); //This gives me 0.000000 :( Hey, green smilies work! :D Awesome.
    }
    Er...Output:

    Welcome to the App Store
    Products:
    G-GPS navigation $4.99
    E-Flashlight $0.99
    E-EBay autobidder $12.99
    R-Resturaunt locator $2.99
    P-Game package $9.99
    Please enter a selection:g
    g╠
    0.000000This item costs $892221753.000000Press any key to continue . . .
    And thanks for the help, I was stuck. If I can get these parts working theoretically the rest of the program that I'm required to make would just be more of the same kinda, only not. But basically I shouldn't have any problems if I can get this part working I think hopefully.
    Last edited by inverse9; 03-27-2011 at 10:57 PM.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    void GetCost(char purchase, double *item_cost)
    {
    	if (purchase=='g')
    		*item_cost='4.99';
    	else
    		*item_cost='5.99';
    	printf("%f", item_cost); //This gives me 0.000000 :( Hey, green smilies work! :D Awesome.
    }
    Turn on your compiler warnings, and it should be telling you that the printf line is wrong. You are printing a memory address, not the value at that address.


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

  6. #6
    Registered User
    Join Date
    Mar 2011
    Posts
    13
    Quote Originally Posted by quzah View Post
    Turn on your compiler warnings, and it should be telling you that the printf line is wrong. You are printing a memory address, not the value at that address.


    Quzah.
    The only warning I get is about how using scanf is unsafe. And yes, I can't seem to find my memory. Please someone tell me how I'm supposed to do the super-simple write things in memory here and from somewhere else retrieve that thingy, since my class notes, paperweight book, and google are conspiring to make me feel stupid. I made the title "Moron newbie" for a reason, since from your actually-experienced programer viewpoints I'm a total idiot.

  7. #7
    Registered User
    Join Date
    Mar 2010
    Posts
    583
    Quite a few of your scanfs and printfs have the wrong level of indirection in them.

    Code:
    void Menu(char *purchase)
    {
    	printf("Products:\nG-GPS navigation $4.99\nE-Flashlight $0.99\nE-EBay autobidder $12.99\n");
    	printf("R-Resturaunt locator $2.99\nP-Game package $9.99\nPlease enter a selection:");
            // this is wrong. Scanf expects a pointer to some memory. purchase is a pointer, so you don't need the & here. Using & makes the type char**.
            scanf("%c", &purchase);
           // also wrong -- should use * to dereference the pointer here.
    	printf("%c", purchase);//This prints the letter I enter.
    }
    Code:
    printf("%f", item_cost); //This gives me 0.000000 :( Hey, green smilies work! :D Awesome.
    quzah mentioned this, you should dereference item_cost. I couldn't get MSVC to give a warning about this either. I don't know if I'd expect it to... since C isn't very strict about types and pointers can be interpreted as integral types (addresses). Don't know. IT'd be nice!

    Last problem, this one you might kick yourself for:
    Code:
    void GetCost(char purchase, double *item_cost)
    {
    	if (purchase=='g')
                    /* item_cost is a double, so you should not have single quotes around the 4.99. You should have no quotes, like *item_cost=4.99; */
    		*item_cost='4.99';
    	else
                    /* Same here */
    		*item_cost='5.99';
    	printf("%f", item_cost); //This gives me 0.000000 :( Hey, green smilies work! :D Awesome.
    }

  8. #8
    Registered User
    Join Date
    Mar 2011
    Posts
    13
    *Head explodes with befuddlement/gratitude*

    Well, I thought I had to do some arcane things to the declarations, but everything works and more importantly I think I kinda understand how this stuff works now. Thanks!

    (50/50 chance I'm gonna be back here in half an hour.)

  9. #9
    Registered User
    Join Date
    Mar 2010
    Posts
    583
    Quote Originally Posted by inverse9 View Post
    *Head explodes with befuddlement/gratitude*

    Well, I thought I had to do some arcane things to the declarations, but everything works and more importantly I think I kinda understand how this stuff works now. Thanks!
    Excellent, glad it's working! It does become natural eventually, honest I think I became happier about it all when I mentally substituted the word "pointer" for "address". The fact that pointers are typed makes this slightly incorrect, but in general it seemed easier to swallow - and it's easier to remember where you should dereference and where you shouldn't. You're certainly not an idiot/moron for not nailing them the first time through.

    (50/50 chance I'm gonna be back here in half an hour.)
    good luck!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. global namespace errors
    By stubaan in forum C++ Programming
    Replies: 9
    Last Post: 04-02-2008, 03:11 PM
  2. Newbie having problems with errors
    By WilliamK99 in forum C++ Programming
    Replies: 3
    Last Post: 04-02-2008, 07:44 AM
  3. Ten Errors
    By AverageSoftware in forum Contests Board
    Replies: 0
    Last Post: 07-20-2007, 10:50 AM
  4. executing errors
    By s0ul2squeeze in forum C++ Programming
    Replies: 3
    Last Post: 03-26-2002, 01:43 PM