1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

question on my definition

Discussion in 'C++' started by kjedmondson182, Oct 12, 2009.

  1. kjedmondson182

    kjedmondson182 New Member

    Joined:
    Oct 10, 2009
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    im trying to complete my getData definition while avoiding an infinate loop
    my error right now is that
    error C4716: 'getData' : must return a value
    i think i need an if statement? can someone show me an example or something?
    Code:
    [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]#include[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]<iostream>
    [/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]#include[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]<iomanip>
    [/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]#include[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]<string>
    [/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]using[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]namespace[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] std;
    [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]struct[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] menuItemType
    {
    string menuItem;
    [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]double[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] menuPrice; 
    [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] numOrdered;
    };
    menuItemType theMenu[8];
    [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]void[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] showMenu();
    [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] getData([/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] choice); 
    [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]double[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] Round([/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]double[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] &totalTax); 
    [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]void[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] printCheck([/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]double[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] &tax, [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]double[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] &totalBill); 
    [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] choice=0; [/SIZE][SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]// The number the user chooses for the item they want 
    [/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] number=0; [/SIZE][SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]// Used in the getData function
    [/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] x=0; [/SIZE][SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]// used in "for" loop to initialize numOrdered to 0.
    [/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]double[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] bill = 0.0; 
    [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]double[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] totalBill = 0.0; [/SIZE][SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]// w/ tax
    [/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]double[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] tax = 0.0; 
    [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]double[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] taxRate = .05; [/SIZE][SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]// tax% 
    [/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]double[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] totalTax =0.0; 
    [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]char[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] shouldContinue; 
    
     
    [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] main()
    { 
    
    theMenu[0].menuItem = [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"#1 Plain Egg"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]; 
    theMenu[1].menuItem = [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"#2 Bacon and Egg"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2];
    theMenu[2].menuItem = [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"#3 Muffin"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2];
    theMenu[3].menuItem = [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"#4 French Toast"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2];
    theMenu[4].menuItem = [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"#5 Fruit Basket"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2];
    theMenu[5].menuItem = [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"#6 Cereal"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2];
    theMenu[6].menuItem = [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"#7 Coffee"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2];
    theMenu[7].menuItem = [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"#8 Tea"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2];
    theMenu[0].menuPrice = 1.45; 
    theMenu[1].menuPrice = 2.45;
    theMenu[2].menuPrice = 0.99;
    theMenu[3].menuPrice = 1.99;
    theMenu[4].menuPrice = 2.49;
    theMenu[5].menuPrice = 0.69;
    theMenu[6].menuPrice = 0.50;
    theMenu[7].menuPrice = 0.75;
    [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]for[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] (x = 0; x < 8; x++) [/SIZE][SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]// Initialize theMenu[x].numOrdered to all 0's. 
    [/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]{
    theMenu[x].numOrdered = 0;
    }
    
    [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]do
    [/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]{
    showMenu(); [/SIZE][SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]// run the void showMenu() function
    [/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]cout << [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"\n"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] << endl;
    choice = getData(number); [/SIZE][SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]// Returns the number the person chose the the array number
    [/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]bill = theMenu[choice].menuPrice + bill; [/SIZE][SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]// Keeps a running total of the bill
    [/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]
    theMenu[choice].numOrdered++; [/SIZE][SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]//Add 1 to the amount of the item ordered. 
    [/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]cout << [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"\n"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] << theMenu[choice].numOrdered << setw (20) << theMenu[choice].menuItem << [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]" \n"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] << endl;
    cout << [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"Would you like to place another order? (Y/N)"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] << endl; [/SIZE][SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]// Allow to purchase more
    [/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]cin >> shouldContinue;
    }
    [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]while[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] (shouldContinue == [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]'Y'[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] || shouldContinue == [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]'y'[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]);
    cout << endl;
    totalTax = bill * taxRate;
    tax = Round(totalTax);
    
    totalBill = bill + tax;
    printCheck(tax, totalBill);
    [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]return[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] 0;
    } 
     
     
    [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]void[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] showMenu()
    {
    [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] x;
    [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]for[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] (x = 0; x <=8; x++)
    cout << theMenu[x].menuItem << [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"\t"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] << theMenu[x].menuPrice << endl; 
    }
     
    [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] getData([/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] choice)
    {
    [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]while[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2](choice < 0 || choice > [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]sizeof[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2](theMenu)/[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]sizeof[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2](menuItemType))
    {
    showMenu(); [/SIZE][SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]// run the void showMenu() function
    [/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]cout << [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"\n"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] << choice << endl;
    [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] choice = getData(number); [/SIZE][SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]// Returns the number the person chose the the array number
    [/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]}
    }
    [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]double[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] Round([/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]const[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]double[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] &totalTax) [/SIZE][SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]//Round to the nearest cent. 
    [/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]{
    [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]double[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] tax;
    
    tax = totalTax * 100.0 + 0.5;
    [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]return[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] tax / 100.0;
    }
    [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]void[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] printCheck([/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]double[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] &tax, [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]double[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] &totalBill)
    {
    [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] x;
    x = 0;
    cout << [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"Welcome \n"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] << endl;
    [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]for[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] (x = 0; x < 8; x++)
    {
    [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]if[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] (theMenu[x].numOrdered > 0)
    {
    cout << theMenu[x].numOrdered << [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"\t"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] << theMenu[x].menuItem << [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"\t"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] << right << theMenu[x].menuPrice << right << endl; 
    }
    [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]else[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] 
    {
    cout << [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]""[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2];
    }
    }
    cout << [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"Tax"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] << setw (27) << tax << endl;
    cout << [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"Amount Due"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] << setw (20) << totalBill << endl;
    }
    
    [/SIZE]
     
  2. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    The error means that it should return a value but doesn't. The statement you need is "return <something>;" (not literally, of course, you have to replace <something> with something appropriate).

    getData calls itself, is that really what you want (recursion)? Recursion is not the same as returning to the top of the function. What do you want getData to do, and how do you want it to use the choice parameter?
     

Share This Page