Compiles successfully but calculations give wrong answer

Can anyone please tell me what's going on here. I'm using MSVC++ and it builds sucessfully but somewhere something is obviously messed it. Once i try to debug it just shows weird numbers.
int k=0; //could this be the problem??? since 0 would be over writing any given value. but if i leave int k; it showsa warning and MSVC would count that as an error.

Any help would be mush appreciated. Thanks



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#include "stdafx.h" //Header file section

#include <iostream>

#include <iomanip>

#include <string>

using namespace std;

//structure declaration

struct menuitemtype

{ string menuitem[10];

float menuprice[10];

};

//function prototype declaration

void getdata (menuitemtype &m, int &c);

void showmenu();

void printcheck(menuitemtype &m,int c);

int main() {//start main

menuitemtype menulist;

int c;

showmenu();

getdata (menulist, c);

printcheck(menulist, c);

system ("pause");

}//End function

//function definations

void showmenu()

{ cout<<"Welcome to Hasib Resturant"<<endl<<endl; 

cout<<"                  Menu                   "<<endl<<endl;

cout<<"1  Plain Egg"<<"\t\t"<<"$1.45"<<endl<<endl;
	
cout<<"2  Bacon and Egg"<<"\t"<<"$2.45"<<endl<<endl;

cout<<"3  Muffin"<<"\t\t"<<"$0.99"<<endl<<endl;

cout<<"4  French Toast"<<"\t\t"<<"$1.99"<<endl<<endl;

cout<<"5  Fruit basket"<<"\t\t"<<"$2.49"<<endl<<endl;

cout<<"6  Cereal"<<"\t\t"<<"$0.69"<<endl<<endl;

cout<<"7  Coffee"<<"\t\t"<<"$0.50"<<endl<<endl;

cout<<"8  Tea"<<"\t\t\t"<<"$0.75"<<endl<<endl;

cout<<"*************************"<<endl;

}//End of showmenu

void getdata(menuitemtype &m ,int &c)

{ int k=0; int choice;

cout<<"Please enter the total number of items selected by customer"<<endl;

cout<<"Please choose any number between 1 and 8 only, Thank You"<<endl;

cin>>c;

cout<<"Your total is:  ";

cin>>choice;

switch (choice)

{ case 1:m.menuitem[k]="Plain Egg";

m.menuprice[k]=1.45f; break;

case 2:m.menuitem[k]="Bacon and Egg";

m.menuprice[k]=2.45f; break;

case 3:m.menuitem[k]="Muffin";

m.menuprice[k]=0.99f;

break;

case 4:m.menuitem[k]="FrenchToast";

m.menuprice[k]=1.99f; break;

case 5:m.menuitem[k]="FruitBasket";

m.menuprice[k]=2.49f; break;

case 6:m.menuitem[k]="cereal";

m.menuprice[k]=0.69f; break;

case 7:m.menuitem[k]="coffee";

m.menuprice[k]=0.50f; break;

case 8:m.menuitem[k]="Tea";

m.menuprice[k]=0.75f; break;

}

}//End get data

void printcheck(menuitemtype &m,int c)

{ int j; float price=0.0f, tax=0.0f, total;

cout<<"          "<<endl;

for(j=0; j<c;j++);

{cout<<j<<" "<<m.menuitem[j]<<" "<<m.menuprice[j]<<endl;

price=price+m.menuprice[j];

}

tax=0.05f*price;

total= tax + price;

cout<<setprecision(2);

cout<<"Tax="<<tax<<endl;

cout<<"Your amount due ="<<total<<endl; }

//End of printcheck. 
Last edited on
Use code tags (click on Edit, Select all of your code and press the <> button on the right)
You are writing to menuitem.<>[0] in getdata() but reading from 0 to c in printcheck()
I suppose there was meant a loop in getdata()
I'm sorry, I'm not catching it? 0 is what throwing me off. I think 0 is the one creating the problem. since int k=0 would be over writing the menu price item and messing up the calculations.

But if i leave it only as int k;
then visuall c++ throws an error stating k is unitialized?

Any solutions you can think off?
int k = 1. Mind blown
lol at mind blown. Although thanks alot. It certainly helped alot. now if the person is ordering only one item it executes the code correctly. but say he/she wants 2 or more items it goes back to the same bizare numbers? Am i doing the calculation wrong or is the code need modification? I did try int k =1; 2; 3;....8; but same results.

Thanks
what is

#include "stdafx.h".

Can you post so I can see?
It's a visual studio default header whose only purpose is to include other files.

Back to the topic: Why not storing a table of items so you won't need all those switch/case's?
That makes it easier to spot the problem, but it's probably some operation skipping/overriding somewhere.
---
Yes, operation skipping because no matter user input, getdata only asks once what the user wants to buy. You should put a loop after the cin of c.
Last edited on
Thanks for your advice. I edit the code and restarted from start and i agree the whole switches was rather jumbling the code. Anywho i finally got everything sorted out.

Here's my new code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include "stdafx.h"
#include<iostream>
#include<string>
#include<iomanip>
using namespace std;
struct menuItemType
{string menuItem;
 double menuPrice;  
};
//function prototype declaration

void getdata( menuItemType[],menuItemType[],int&);
void showMenu( menuItemType[],int);
void printCheck(menuItemType[],int );
int main()
{
int items=0;  //write down all menu items and their respective prices 
    menuItemType menuList[]={"plain egg",1.45, 

                          "bacon and egg",2.45,

                          "muffin",0.99,

                          "french toast",1.99,

                          "fruit basket",2.49,

                          "cereal",0.69,

                          "coffee",0.50,

                          "tea",0.75

                          };
menuItemType order[10]; //write a welcome message
cout<<"Welcome to Hasib's Restaurant\n\n Please choose from the menu below\n\n     Item\t     price\n\n"; 
showMenu(menuList,8); //show menu from the data listed above
getdata(menuList,order,items);
printCheck(order,items);
system("pause");
return 0;
}
void printCheck(menuItemType order[],int items)
{int i;
double total=0,tax;
 cout<<"Your Ordered Items are:\n     Item\t     price\n"; //confirm the items ordered on a single check
 showMenu(order,items);
 for(i=0;i<items;i++)
     total+=order[i].menuPrice;
 cout<<"\nItem total    \t"<<"$"<<setprecision(2)<<fixed<<total<<endl;
 tax=total*.05; //add a 5% tax to the subtotal before prinitng receipt
 cout<<"Tax\t\t"<<"$"<<setprecision(2)<<fixed<<tax<<endl; //show the amount of tax
 cout<<"Amount Due    \t"<<"$"<<setprecision(2)<<fixed<<total+tax<<endl; //print the total amount after tax is added to the subtotal(for all the items on single order)
}
void getdata(menuItemType menu[],menuItemType order[],int& items)
{char yesno='Y';
int n;
while(toupper(yesno)=='Y')
{cout<<"Please enter your order item number below:\n "; //ask customer for order number and enter them below
cin>>n;
while(n<1||n>8)
    {cout<<"invalid item number\n\n"; //if item number is not on the menu, show an error message and ask for another input
     cout<<"Please choose any number between 1 and 8 only:\n "; //ask again for numbers given on the menu only
     cin>>n;
     }
order[items].menuItem=menu[n-1].menuItem;
order[items].menuPrice=menu[n-1].menuPrice;
items++;
cout<<"Would you like to add another item? \t (y/n)?\n "; //ask if customer wants to add more, if yes add to the order and recalculate the price at the end, if no calulate the appropriate amount
cin>>yesno;
}
}
void showMenu(menuItemType a[],int n)
{int i;
 
    for(i=0;i<n;i++)
         cout<<i+1<<".  "<<setw(16)<<left<<a[i].menuItem<<"$"<<setprecision(2)<<fixed<<a[i].menuPrice<<endl;
} 

||=== cplusplus stuff, Debug ===|
s C++\cplusplus stuff\main.cpp|1|error: stdafx.h: No such file or directory|
s C++\cplusplus stuff\main.cpp||In function 'int main()':|
s C++\cplusplus stuff\main.cpp|34|warning: missing braces around initializer for 'menuItemType'|
s C++\cplusplus stuff\main.cpp|34|warning: missing braces around initializer for 'menuItemType'|
s C++\cplusplus stuff\main.cpp|34|warning: missing braces around initializer for 'menuItemType'|
s C++\cplusplus stuff\main.cpp|34|warning: missing braces around initializer for 'menuItemType'|
s C++\cplusplus stuff\main.cpp|34|warning: missing braces around initializer for 'menuItemType'|
s C++\cplusplus stuff\main.cpp|34|warning: missing braces around initializer for 'menuItemType'|
s C++\cplusplus stuff\main.cpp|34|warning: missing braces around initializer for 'menuItemType'|
s C++\cplusplus stuff\main.cpp|34|warning: missing braces around initializer for 'menuItemType'|
s C++\cplusplus stuff\main.cpp|40|error: 'system' was not declared in this scope|
||=== Build finished: 2 errors, 8 warnings ===|
Errors
I'm not exactly sure what compiler you're using. But in microsoft visual c++ everything works perfectly.

stdafx.h // is a normal heading file for visual c++

and i'm not sure why it is throwing all those errors. i tried it on codepad and it throws those errors but dev c, it works just fine.
On those lines you need an extra set of {} around each pair that makes up each structure.

Also, don't use system.
closed account (S6k9GNh0)
stdafx.h isn't a standard header. It's used to precompile headers in order to speed up compilation time. The name "stdafx" doesn't mean much in particular. I honestly don't see the need for the complication or the speedup given modern technology and compilation techniques.

For more info: http://en.wikipedia.org/wiki/Precompiled_header#stdafx.h
Last edited on
Thanks for all the help guys, much appreciated!!!
Topic archived. No new replies allowed.