Thread: Variables Passed Into A Function, Then Compared (</>/=) In While Loop- Error

  1. #1
    Registered User
    Join Date
    Oct 2011
    Location
    ~Sacramento, CA
    Posts
    14

    Variables Passed Into A Function, Then Compared (</>/=) In While Loop- Error

    Code:
    int GetInt(int IntUsersNumber, int IntLowRange, int IntHighRange)
          {
                 int IntUsersNewNumber;
                 
                 while (IntUsersNumber < 'IntLowRange' || IntUsersNumber > 'IntHighRange')
                 {
                 printf("Out of range, try again");
                 scanf("%d%*c", &IntUsersNewNumber);
                 
                 return IntUsersNewNumber;
                 }
          }
    It says:
    [Warning] Character constant too long for its type.

    It must be the IntLowRange and IntHighRange- but what to do with them...

    Thanks in advance
    Last edited by FiringBlanks; 10-19-2011 at 11:28 PM.

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    In the while condition, there are extraneous single quote characters. Hence the compiler warning.

    If you remove those single quote characters, the logic of the code is still broken. The first time through the loop, it will read a value to IntUsersNewNumber (a different variable) and then return that value. The code will certainly not keep looping until it gets a valid value.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Oct 2011
    Location
    ~Sacramento, CA
    Posts
    14
    lol nice signature grumpy.

    Well this function runs only to validate a number entered in by the user to see if it falls into a certain range. If it doesn't, it asks for a new number that does fall into that range and should return that value into the GetInt function, which is being called by the Main function. If you already knew that- then I'm not quite sure I understand how to remedy this.

    I appreciate the help

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    IntLowRange and IntHighRange are numbers passed into the function ... remove the apostophes from the while statement and see what happens.

    also your scanf() call is a bit wonky... try ... scanf(" %d", &IntUsersNewNumber); instead.

  5. #5
    Registered User
    Join Date
    Oct 2011
    Location
    ~Sacramento, CA
    Posts
    14
    I've taken the apostrophes out- but shouldn't you tell the compiler to discard the enter key after the user enters their number? (hence, the %*c in scanf)

    It's now held up on the errors:
    [Linker Error] Undefined Reference to '...'

    where i put '...' is the name of all the functions i've used throughout this program (same error message for each). I've never got these kinds of errors before.

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    The general method for telling scanf() to ignore whitespace before an entry is to include a leading space in the formatting string...
    Look closely...
    Code:
    scanf(" %c",&character);
    The linker errors are because you aren't telling it what libraries to link with... You will need to look up the functions listed and find out which libraries they are in and add them to your linker's list.

    If you would post a decent amount of information ... i.e. the names of the functions... we might be able to help you. Far as I know not one of us has graduated Bertha's International Academy of Mind Readers.

  7. #7
    Registered User
    Join Date
    Oct 2011
    Location
    ~Sacramento, CA
    Posts
    14
    You will need to look up the functions listed and find out which libraries they are in and add them to your linker's list.
    Hmm... the only odd looking function I'm using is the 'switch' funciton- does that require a special library? Other than that, theres also a 'while' function in there.

  8. #8
    Registered User
    Join Date
    Oct 2011
    Location
    ~Sacramento, CA
    Posts
    14
    If you would post a decent amount of information ... i.e. the names of the functions... we might be able to help you. Far as I know not one of us has graduated Bertha's International Academy of Mind Readers.
    Code:
     /* Fahad Shah
        Lab 5
        10-5-11
        Functions*/
    
    
    #include <stdio.h>
    
    /*Prototypes*/
    int Menu(int UsersMenuChoice);
    double Add(double AddVar);
    double GetReal(double DoubleUsersNumber, double DoubleLowRange, double DoubleHighRange);
    int GetInt(int IntUsersNumber, int IntLowRange, int IntHighRange);
    double Total(double TotalVar1, double TotalVar2);
    void Show(int ProdNum, int ProdQuant, double ProdCost, double ProdPrice);
    
    /*------------------------------------------------*/
    
    int main()
    {      
                  int MenuChoice;
                  char Terminate;
          
          do
          { 
                           MenuChoice = Menu(0);
                           
                           switch (MenuChoice)
                           {
                           case 1: add();
                                break;
                           case 5: return 0;
                           }
                           
                           Terminate = menu(MenuChoice);
          }while (Terminate != 5);
          }
          
    
    
    /*------------------------------------------------*/
          
          /*
          -----------------------------
          Functions
          -----------------------------
          */
          
          
          int Menu(UsersMenuChoice)
          {
                int MenuChoiceNumber;
                
                printf("Sierra Sporting Goods\n\n");
                printf("1 = Add A Record\n");
                printf("2 = Report\n");
                printf("3 = Delete A Record\n");
                printf("4 = Change A Record\n");
                printf("5 = Quit\n");
                
                scanf("%d%*c", MenuChoiceNumber);
                
                return MenuChoiceNumber;
             }
             
             
    /*------------------------------------------------*/
          
          double Add(double AddVar)
          {
               
               int ProdNum, ProdType, ProdQuant;
               double ProdCost, ProdPrice;
               char Terminate;
               
               do{  
                    
                          /* Getting Information From The User*/
          printf("Enter The Product Number\n");
          scanf("%d%*c", &ProdNum);
          GetInt(ProdNum, 1, 9999);
          
          printf("Enter The Product Type\n");
          scanf("%d%*c", &ProdType);
          GetInt(ProdType, 1, 5);
    
          printf("Enter The Quantity\n");
          scanf("%d%*c", &ProdQuant);
          GetInt(ProdQuant, 0, 50);
    
          printf("Enter The Cost\n");
          scanf("%lf%*c", &ProdCost);
          GetReal(ProdCost, 5, 900);
    
          printf("Enter The Price\n");
          scanf("%lf%*c", &ProdPrice);
          GetReal(ProdPrice, 6, 1000);
          
          Show(ProdNum, ProdQuant, ProdCost, ProdPrice);
          
          /*End- Continue?*/
          printf("Continue? y/n \n\n");
          scanf("%c%*c", &Terminate);
          
          }while (Terminate != 'N' || Terminate != 'n');
    
          }
    /*------------------------------------------------*/
          
          int GetInt(int IntUsersNumber, int IntLowRange, int IntHighRange)
          {
                 int IntUsersNewNumber;
                 
                 while (IntUsersNumber < IntLowRange || IntUsersNumber > IntHighRange)
                 {
                 printf("Out of range, try again");
                 scanf("%d%*c", &IntUsersNewNumber);
                 
                 return IntUsersNewNumber;
                 }
          }
          
    /*------------------------------------------------*/
          
          double GetReal(double DoubleUsersNumber, double DoubleLowRange, double DoubleHighRange)
          {
                 int DoubleUsersNewNumber;
                 
                 while (DoubleUsersNumber < DoubleLowRange || DoubleUsersNumber > DoubleHighRange)
                 {
                 printf("Out of range, try again");
                 scanf("%d%*c", &DoubleUsersNewNumber);
                 
                 return DoubleUsersNewNumber;
          }
          
    /*------------------------------------------------*/
          
          double Total(double TotalVar1, double TotalVar2)
          {
                 return TotalVar1*TotalVar2;
          }
    
    /*------------------------------------------------*/
    
          void Show(int ProdNum, int ProdQuant, double ProdCost, double ProdPrice)
          {
               int TotalCost, TotalPrice;
               
                 TotalPrice = total(ProdQuant, ProdCost); /*Total Cost*/
                 printf("Total Cost: %d", TotalCost);
                 
                 total(ProdQuant, ProdPrice); /* Total Price*/
                 printf("Total Price: %d", TotalPrice);
          }
    
          
    }
    I'm using bloodshed and it's showing a linker error for every function i created

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Ok... you seriously need to start listening to compiler errors... Either set DevC++ error reporting level to maximum or get a better compiler (that one is probably 6 years out of date).



    You prototype Add()... which you are trying to call as add() on line 30.
    You also prototyped Menu() which you called as menu() on line 35

    Fixing the capitalization on line 30 yeilds an error for incorrect parameters... You prototyped Add() to take a double value, but in Add() itself you never use the value... so you might as well change that to Add(void);

    In Add() you prototyped to return a double, then you don't return anything. Moerover; at line 30, your call to Add() when fixed would ignore the return value.

    Your function Menu() is prototyped to accept an int, but on line 50 you don't show the type so the call doesn't match the prototype, moreover you never actually use this variable inside the function.

    Menu() returns an int and at line 35 when you try to assign it to a char value.

    you have a brace mismatch beginning at line 125 that renders the rest of the code indecipherable by the compiler.

    There's more... here for reference are the compiler errors from Pelles C...

    PHP Code:

    Building main
    .obj.
    D:\Programming\C_Code\Experiments\junk\main.c(20): warning #2117: Old-style function definition for 'main'.
    D:\Programming\C_Code\Experiments\junk\main.c(30): warning #2027: Missing prototype for 'add'.
    D:\Programming\C_Code\Experiments\junk\main.c(30): warning #2216: The return value from 'add' is never used.
    D:\Programming\C_Code\Experiments\junk\main.c(35): warning #2027: Missing prototype for 'menu'.
    D:\Programming\C_Code\Experiments\junk\main.c(35): warning #2215: Conversion from 'int' to 'char'; possible loss of data.
    D:\Programming\C_Code\Experiments\junk\main.c(51): warning #2117: Old-style function definition for 'Menu'.
    D:\Programming\C_Code\Experiments\junk\main.c(54): warning #2216: The return value from 'printf' is never used.
    D:\Programming\C_Code\Experiments\junk\main.c(55): warning #2216: The return value from 'printf' is never used.
    D:\Programming\C_Code\Experiments\junk\main.c(56): warning #2216: The return value from 'printf' is never used.
    D:\Programming\C_Code\Experiments\junk\main.c(57): warning #2216: The return value from 'printf' is never used.
    D:\Programming\C_Code\Experiments\junk\main.c(58): warning #2216: The return value from 'printf' is never used.
    D:\Programming\C_Code\Experiments\junk\main.c(59): warning #2216: The return value from 'printf' is never used.
    D:\Programming\C_Code\Experiments\junk\main.c(61): warning #2234: Argument 2 to 'scanf' does not match the format string; expected 'int *' but found 'int'.
    D:\Programming\C_Code\Experiments\junk\main.c(61): warning #2216: The return value from 'scanf' is never used.
    D:\Programming\C_Code\Experiments\junk\main.c(50): warning #2118: Parameter 'UsersMenuChoice' is not referenced.
    D:\Programming\C_Code\Experiments\junk\main.c(52): warning #2116: Local 'MenuChoiceNumber' is used without being initialized.
    D:\Programming\C_Code\Experiments\junk\main.c(79): warning #2216: The return value from 'printf' is never used.
    D:\Programming\C_Code\Experiments\junk\main.c(80): warning #2216: The return value from 'scanf' is never used.
    D:\Programming\C_Code\Experiments\junk\main.c(81): warning #2216: The return value from 'GetInt' is never used.
    D:\Programming\C_Code\Experiments\junk\main.c(83): warning #2216: The return value from 'printf' is never used.
    D:\Programming\C_Code\Experiments\junk\main.c(84): warning #2216: The return value from 'scanf' is never used.
    D:\Programming\C_Code\Experiments\junk\main.c(85): warning #2216: The return value from 'GetInt' is never used.
    D:\Programming\C_Code\Experiments\junk\main.c(87): warning #2216: The return value from 'printf' is never used.
    D:\Programming\C_Code\Experiments\junk\main.c(88): warning #2216: The return value from 'scanf' is never used.
    D:\Programming\C_Code\Experiments\junk\main.c(89): warning #2216: The return value from 'GetInt' is never used.
    D:\Programming\C_Code\Experiments\junk\main.c(91): warning #2216: The return value from 'printf' is never used.
    D:\Programming\C_Code\Experiments\junk\main.c(92): warning #2216: The return value from 'scanf' is never used.
    D:\Programming\C_Code\Experiments\junk\main.c(93): warning #2216: The return value from 'GetReal' is never used.
    D:\Programming\C_Code\Experiments\junk\main.c(95): warning #2216: The return value from 'printf' is never used.
    D:\Programming\C_Code\Experiments\junk\main.c(96): warning #2216: The return value from 'scanf' is never used.
    D:\Programming\C_Code\Experiments\junk\main.c(97): warning #2216: The return value from 'GetReal' is never used.
    D:\Programming\C_Code\Experiments\junk\main.c(102): warning #2216: The return value from 'printf' is never used.
    D:\Programming\C_Code\Experiments\junk\main.c(103): warning #2216: The return value from 'scanf' is never used.
    D:\Programming\C_Code\Experiments\junk\main.c(69): warning #2118: Parameter 'AddVar' is not referenced.
    D:\Programming\C_Code\Experiments\junk\main.c(69): warning #2096: Missing return value.
    D:\Programming\C_Code\Experiments\junk\main.c(116): warning #2216: The return value from 'printf' is never used.
    D:\Programming\C_Code\Experiments\junk\main.c(117): warning #2216: The return value from 'scanf' is never used.
    D:\Programming\C_Code\Experiments\junk\main.c(110): warning #2235: Not all control paths return a value.
    D:\Programming\C_Code\Experiments\junk\main.c(131): warning #2216: The return value from 'printf' is never used.
    D:\Programming\C_Code\Experiments\junk\main.c(132): warning #2216: The return value from 'scanf' is never used.
    D:\Programming\C_Code\Experiments\junk\main.c(134): warning #2215: Conversion from 'int' to 'double'; possible loss of data.
    D:\Programming\C_Code\Experiments\junk\main.c(140): error #2001: Syntax error: expected ';' but found '{'.
    D:\Programming\C_Code\Experiments\junk\main.c(141): error #2048: Undeclared identifier 'TotalVar1'.
    D:\Programming\C_Code\Experiments\junk\main.c(141): error #2048: Undeclared identifier 'TotalVar2'.
    D:\Programming\C_Code\Experiments\junk\main.c(150): warning #2027: Missing prototype for 'total'.
    D:\Programming\C_Code\Experiments\junk\main.c(151): warning #2216: The return value from 'printf' is never used.
    D:\Programming\C_Code\Experiments\junk\main.c(153): warning #2216: The return value from 'total' is never used.
    D:\Programming\C_Code\Experiments\junk\main.c(154): warning #2216: The return value from 'printf' is never used.
    D:\Programming\C_Code\Experiments\junk\main.c(146): warning #2118: Parameter 'ProdNum' is not referenced.
    D:\Programming\C_Code\Experiments\junk\main.c(146): error #2156: Unrecognized declaration.
    *** Error code***
    Done
    When you get a mess of errors like this, the only thing to do is start at the first one and work through the list...
    Last edited by CommonTater; 10-21-2011 at 04:17 AM.

  10. #10
    Registered User
    Join Date
    Oct 2011
    Location
    ~Sacramento, CA
    Posts
    14
    Ok... you seriously need to start listening to compiler errors... Either set DevC++ error reporting level to maximum or get a better compiler (that one is probably 6 years out of date).
    I also have Visual Studio 2010 but it gives me odd errors that otherwise work fine in Bloodshed. Also, I'm not sure if it's just me- but I find the compiler's error definitions cryptic and point to the problem in an around/about way.

    When you get a mess of errors like this, the only thing to do is start at the first one and work through the list...
    I'm still learning the dynamics of programming- I don't really understand a lot of what you can and cannot do.

    Should I give Menu() an int type instead of void since it is reading from int MenuChoice? Or should I just get rid of MenuChoice altogether in main() and try doing:
    Code:
    int main()
    
     {      
    
           do
           { 
    
                             
                            switch (menu())
    
                            {
                            case 1: Add();
    
                                 break;
                            case 5: return 0;
                            }
    
           }while (Menu()!=5);
    
           }
    I couldn't find the brace mismatch on line 125 and tried to give Add() a void type but it gives me an error. Here's what I have now:

    Code:
    /* Fahad Shah
        Lab 5
        10-5-11
        Functions*/
    
    
    #include <stdio.h>
    
    /*Prototypes*/
    int Menu(int UsersMenuChoice);
    int Add(void);
    double GetReal(double DoubleUsersNumber, double DoubleLowRange, double DoubleHighRange);
    int GetInt(int IntUsersNumber, int IntLowRange, int IntHighRange);
    double Total(double TotalVar1, double TotalVar2);
    void Show(int ProdNum, int ProdQuant, double ProdCost, double ProdPrice);
    
    /*------------------------------------------------*/
    
    int main()
    {      
                  int MenuChoice;
                  char Terminate;
          
          do
          { 
                           MenuChoice = Menu(0);
                           
                           switch (MenuChoice)
                           {
                           case 1: Add(void);
                                break;
                           case 5: return 0;
                           }
                           
                           Terminate = Menu(MenuChoice);
          }while (Terminate != 5);
          }
          
    
    
    /*------------------------------------------------*/
          
          /*
          -----------------------------
          Functions
          -----------------------------
          */
          
          
          int Menu(int UsersMenuChoice)
          {
                int MenuChoiceNumber;
                
                printf("Sierra Sporting Goods\n\n");
                printf("1 = Add A Record\n");
                printf("2 = Report\n");
                printf("3 = Delete A Record\n");
                printf("4 = Change A Record\n");
                printf("5 = Quit\n");
                
                scanf("%d%*c", MenuChoiceNumber);
                
                return MenuChoiceNumber;
             }
             
             
    /*------------------------------------------------*/
          
          void Add(void)
          {
               
               int ProdNum, ProdType, ProdQuant;
               double ProdCost, ProdPrice;
               char Terminate;
               
               do{  
                    
                          /* Getting Information From The User*/
          printf("Enter The Product Number\n");
          scanf("%d%*c", &ProdNum);
          GetInt(ProdNum, 1, 9999);
          
          printf("Enter The Product Type\n");
          scanf("%d%*c", &ProdType);
          GetInt(ProdType, 1, 5);
    
          printf("Enter The Quantity\n");
          scanf("%d%*c", &ProdQuant);
          GetInt(ProdQuant, 0, 50);
    
          printf("Enter The Cost\n");
          scanf("%lf%*c", &ProdCost);
          GetReal(ProdCost, 5, 900);
    
          printf("Enter The Price\n");
          scanf("%lf%*c", &ProdPrice);
          GetReal(ProdPrice, 6, 1000);
          
          Show(ProdNum, ProdQuant, ProdCost, ProdPrice);
          
          /*End- Continue?*/
          printf("Continue? y/n \n\n");
          scanf("%c%*c", &Terminate);
          
          }while (Terminate != 'N' || Terminate != 'n');
    
          }
    /*------------------------------------------------*/
          
          int GetInt(int IntUsersNumber, int IntLowRange, int IntHighRange)
          {
                 int IntUsersNewNumber;
                 
                 while (IntUsersNumber < IntLowRange || IntUsersNumber > IntHighRange)
                 {
                 printf("Out of range, try again");
                 scanf("%d%*c", &IntUsersNewNumber);
                 
                 return IntUsersNewNumber;
                 }
          }
          
    /*------------------------------------------------*/
          
          double GetReal(double DoubleUsersNumber, double DoubleLowRange, double DoubleHighRange)
          {
                 int DoubleUsersNewNumber;
                 
                 while (DoubleUsersNumber < DoubleLowRange || DoubleUsersNumber > DoubleHighRange)
                 {
                 printf("Out of range, try again");
                 scanf("%d%*c", &DoubleUsersNewNumber);
                 
                 return DoubleUsersNewNumber;
          }
          
    /*------------------------------------------------*/
          
          double Total(double TotalVar1, double TotalVar2)
          {
                 return TotalVar1*TotalVar2;
          }
    
    /*------------------------------------------------*/
    
          void Show(int ProdNum, int ProdQuant, double ProdCost, double ProdPrice)
          {
               int TotalCost, TotalPrice;
               
                 TotalPrice = total(ProdQuant, ProdCost); /*Total Cost*/
                 printf("Total Cost: %d", TotalCost);
                 
                 total(ProdQuant, ProdPrice); /* Total Price*/
                 printf("Total Price: %d", TotalPrice);
          }
    
          
    }
    I really appreciate the help
    Last edited by FiringBlanks; 10-22-2011 at 04:35 PM.

  11. #11
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by FiringBlanks View Post
    I also have Visual Studio 2010 but it gives me odd errors that otherwise work fine in Bloodshed. Also, I'm not sure if it's just me- but I find the compiler's error definitions cryptic and point to the problem in an around/about way.

    Nothing cryptic or roundabout in it... it's very explicit...
    PHP Code:
    D:\Programming\C_Code\Experiments\junk\main.c(140): error #2001: Syntax error: expected ';' but found '{'. 
    |------------------ filename---------------| line | --------------- error report ----------------------| 


    I'm still learning the dynamics of programming- I don't really understand a lot of what you can and cannot do.
    What you cannot do is make stuff up. The compiler is a flaming idiot, it understands a very limited vocabulary and has to be told *everything* in little tiny steps in the right order. It don't understand you... so you have to understand it.

    Ok... you can spend the next 3 weeks frustrating yourself into submission or you can do the smart thing and grab a good tutorial on C Programming and work through it page by page.... start on page1, read every word, review as necessary, type up the examples and exercises, compile and run the code, play with it... break it, fix it, change it, untill you understand what works and what doesnt... then move on to page2... repeating this process until you hit the end of the book... By then you should be more than qualified to write this program...

    You can get started ... HERE

    One other thing you should do while developing a program, is to work in sections... get the skeletal code to compile (basically every program begins with the "hello world" proggy), then get your menu working... then add your other functions one at a time getting each to work before adding the next... Writing the whole thing at once than fighting with 130 errors almost never works...
    Last edited by CommonTater; 10-22-2011 at 07:02 PM.

  12. #12
    Registered User
    Join Date
    Oct 2011
    Location
    ~Sacramento, CA
    Posts
    14
    I'm writing this for a C programming class- we've worked through everything up to this point- this was last weeks lab assignment and i haven't been able to get it to work, even though i just completed the exam which included this. I would love to spend countless hours reviewing everything unrelated to the specifics of the problem at hand but am working near the deadline of the next assignment.

    thanks anyways

  13. #13
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by FiringBlanks View Post
    I'm writing this for a C programming class- we've worked through everything up to this point- this was last weeks lab assignment and i haven't been able to get it to work, even though i just completed the exam which included this. I would love to spend countless hours reviewing everything unrelated to the specifics of the problem at hand but am working near the deadline of the next assignment.

    thanks anyways
    Then you would do yourself quite the favor by undertaking a review of some of the things you don't seem to grasp particularly well... (no offense, just a bit of advice...)

    You should review...
    Functions, Parameter passing and program Structure... since these appear to be tripping stones for you and will only get worse over time (just wait till you hit pointers!). Errors and Error reports ... that's sort of a quicky (see above) but not all compilers are the same.
    Last edited by CommonTater; 10-22-2011 at 07:57 PM.

  14. #14
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    If you put a return inside your while loop then it's not going to loop.
    And if you don't close your loop then it wont compile.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. int value passed to function
    By search in forum C Programming
    Replies: 7
    Last Post: 03-10-2010, 06:05 PM
  2. Replies: 2
    Last Post: 02-26-2009, 11:48 PM
  3. Replies: 3
    Last Post: 11-17-2008, 12:59 PM
  4. Replies: 9
    Last Post: 05-03-2007, 03:50 PM
  5. Examining flags when passed to a function?
    By DominicTrix in forum C++ Programming
    Replies: 5
    Last Post: 02-01-2003, 01:34 PM

Tags for this Thread