Compiles successfully but calculations give wrong answer

Stewie k (3)
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
EssGeEich (1007)
Use code tags (click on Edit, Select all of your code and press the <> button on the right)
MiiNiPaa (226)
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()
Stewie k (3)
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?
greenleaf800073 (82)
int k = 1. Mind blown
Stewie k (3)
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
greenleaf800073 (82)
what is

#include "stdafx.h".

Can you post so I can see?
EssGeEich (1007)
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
Registered users can post here. Sign in or register to post.