Different Outputs for C++ Project (used atoi and atof)


Here is my input file:

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

//CODE:

// Local Restaurant: The 5,000 Fed

#include <cmath>
#include <cstdlib>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <string>

using namespace std;

struct menuItemType {
string menuItem;
double menuPrice;
};

void getData(menuItemType list[]);
string showMenu(menuItemType list[]);
void printCheck(string y, menuItemType list[]);

int main() {
string y;

menuItemType menuList[8];
cout << "Hello and welcome to the 5,000 fed. Your local Long Beach café, "
"whose name is inspired by Jesus' miraculous manifestation "
"of bread to feed the 5k! His disciples helped him in passing out "
"the bread, and so I am here, your waiter, to help you and assist "
"you with your order. ";

getData(menuList);
y = showMenu(menuList);
printCheck(y, menuList);

return 0;
}

void getData(menuItemType list[]) {

string menupricet[8];

ifstream inFile;
inFile.open("menudata.txt");
for (int i = 0; i < 8; i++) {
getline(inFile, list[i].menuItem);
getline(inFile, menupricet[i]);
}

for (int i = 0; i < 8; i++) {
list[i].menuPrice = atof(menupricet[i].c_str());
}
}

string showMenu(menuItemType list[]) {

int amount;
char number;
string reps;
string response = "y";
string total = "";

cout << fixed << setprecision(2);
cout << endl << endl;
cout << "Here is our wonderful menu. I do recommend the french toast, "
"similar to the bread that Jesus ate."
<< endl
<< endl;

for (int i = 0; i < 8; i++) {
cout << i + 1 << ". " << setw(13) << left << list[i].menuItem << setw(8)
<< right << list[i].menuPrice << endl;
}
cout << endl;

cout << "How to order: Write the number you would like and specify how many "
"you want of that item by typing it in multiple times. For instance "
"an input of \"1135777\" means that you want two plain eggs, one "
"muffin, one fruit basket, and three coffees."
<< endl
<< endl;

cout << "How many items are you ordering total?: ";
cin >> amount;
cout << endl;

cout << "Please input your order, and press enter after your order: ";

for (int i = 0; i < amount; i++) {
cin >> number;
total += number;
}

return total;
}

void printCheck(string y, menuItemType list[]) {

int k = 0;
int temp = 0;
double bill = 0;
int newone = 0;

k = atoi(y.c_str());

while (k != 0) {
newone = newone * 10;
newone = newone + k % 10;
k = k / 10;
}

cout << "Thank you for dining at the 5,000 fed. Here's your check:" << endl
<< endl
<< endl;

cout << "_________________" << endl;

while (newone > 0) {
temp = newone % 10;
newone /= 10;
bill += list[temp - 1].menuPrice;

cout << left << setw(15) << list[temp - 1].menuItem << right << setw(5)
<< "|" << list[temp - 1].menuPrice << endl;
}

cout << fixed << setprecision(2);
cout << left << setw(15) << "Amount Due:" << right << setw(10) << "|" << bill
<< endl
<< endl;
}








I'm having no problem when people order 8 or 9 items, but as soon as they enter 10 or more items, I get really weird results and not the nicely printed check that I would like to see. For instance, let's say someone orders 8 items and they input "12345678" for their order. I get this:

_________________
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
Amount Due: |11.31

But if the user inputs that they want let's say 15 items and they order "123456781234567", I get THIS:

_________________
French Toast |1.99
\365\277\357\376^ޝ-
YW\300\365\277\357\376all\377 \200\367\277\357\376 \370\277\357\376 \227\370\277\357\376 \331\370\277\357\376%\371\277\357\376\262 |0.00 Coffee |0.50 Muffin |0.99 \362\277\357\376x\362\277\357\376x\362\277\357\376x\362\277\357\376Plain Egg33333 |0.00 Plain Egg |1.45 Cereal |0.69 \365\277\357\376^ޝ-YW\300\365\277\357\376all\377\200\367\277\357\376\370\277\357\376\227\370\277\357\376\331\370\277\357\376%\371\277\357\376\262 |0.00 Cereal |0.69 Amount Due: |6.31

Even when they order 10 items, I get just a blank check :/ :
_________________
Amount Due: |0.00


What in the world is happening with my program? Does it have to do with me using c-strings? I want to understand what is going on inside my little computer and understand the way my computer thinks. If you can explain me this in a very simple way (I'm really new so I need something very explanatory with definitions of your fancy words) and some way I can understand, because like I said, I'm really new to computer science. Thank you.


An int is a whole number.
A double is a (probably non whole) number.
A char is a single character, NOT a number.
A string is sequence of characters.

So you have written:
char number;

string total = "";

total += number;

Hmm.

Please put your code in code tags.
Last edited on
How do I put my code in tags?
(Except on the initial post) you can use the format menu to the right: select your code and click the first button <>. Other format buttons can be used for output, quoting, etc.. There is a known bug on the initial post to the thread that prevents you doing this, but you can immediately go back and edit your post to sort this out.

Alternatively you can just type [code] and [/code] around your code.

This allows us:
(a) to see your code with proper indentation, making it (hopefully) easier to read;
(b) able to refer to line numbers;
(c) run it online in cpp-shell, so not having to go through the rigmarole of downloading it.


The fundamental problem with your code is that you don't appear to recognise the difference between
characters 'a', 'b', 'c', ..., '0', '1', '2', ...
and
numbers 0, 1, 2, ...

The problem starts for you at 10, because '1','0' is two chars, not one. (And you shouldn't be using chars here anyway.)

I suggest that you READ
http://www.cplusplus.com/doc/tutorial/variables/
Last edited on
Topic archived. No new replies allowed.