what can I do here?!

Good day, I'm writing this program of a chocolate machine, and its NOT working! can anyone help me with it?

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
#include <iostream>
#include <fstream>
#include <string>

	double total_price(int order);

		int main()
	{
		using namespace std;

		string menu[8];
		int order, i=0;
		char next, Y, y, N, n;
		double total;

		ifstream in_stream;
		ofstream out_stream;

		in_stream.open("menu.txt");
		out_stream.open("order_lest.dat");

		cout<<"Welcome to e-Chocolate! where you can find all the chocolate you can dream of!\n"
		<<"please enter your choices, knowing that the chocolate is (2.00$) each."
		<<"your first choice:";
		

		for (i=0; i<8; i++)
	{
		order=menu[i];
		cout<<i+1<<menu[i]<<"-"<<endl;
	}

		out_stream<<menu;
		in_stream>>order;

		total=total_price(order);


		cout<<"Do you want anything else?(Y-N)\n";
	cin>>next;
	if (next==Y||next==y)
	{
		cout<<"please enter your choice:";
		cin>>order;

	if (next==N||next==n)
	cout<<"your total is:"<<total<<"\t";
	
	}

		double total_price(int order)
		{
			double total;
			total=order++;
			return total;
		}

		in_stream.close();
		out_stream.close();

	return 0;
		}


THANK YOU SO MUCH !
Firstly, line 9 is meant to be outside of main().

Secondly, line 29 is trying to assign a string from the string array 'menu'(which doesn't actually contain anything) to an int.

Line 41 and 46 are dreadfully wrong, both comparison and use of variables.

Line 51-56 should be below main.

Please refer to the tutorial on this site: http://www.cplusplus.com/doc/tutorial/

EDIT* Sorry, this post makes me sound like an ass...
Last edited on
There is quite a bit wrong with this program, and that's without me trying to compile it.

-For starters lines 19 and 20 do not designate an open mode for menu.txt or order_lest.dat respectfully. I do not know if this will stop it from compiling but it is probably bad practice.

-In line 24 you appear to want to prompt the user for input but do not state in what format to input the choice nor do you have an input of anykind until Line 39. Also add a whitespace after the ':' on Line 24, trust me it will make the input look much better when you fix this.

-Line 29 To read the contents of the menu.txt file into the menu[] array replace "order=menu[i];" with "menu[i] >> in_stream;" without the quotes of course. If you are trying to do something else please inclued comments or some way to clarify.

- Line 33 will either need to be included in the for loop or have a for loop of it's own in order to write the menu array to order_lest.dat; and it will need to know what part of the menu array you are writing to out_stream.

- Line 34 you are just pulling the next input from in_stream and assigning it to the order integer, I am assuming based on Line 29 and the fact that menu is a string array that this will not work no matter how you try to do it because menu.txt contains text (I assume) which will not be assigned to an integer.

There is more but I would suggest rereading the chapter that you are on before going further.
????
Also you should try to keep your indentation consistent, this benefits your debugging process and people on the forum trying to help you.

if ( next==Y ...

There's a couple things you can do with this, namely because you have declared the char's y Y n Y
I assume you should probably initialize them if your going to use them in this way. eg.

char y='y', Y='Y', n='n', N='N' ;

this way your conditional will work, otherwise you can change the conditional to
if ( next == 'y' || next == 'Y' )

char y; is a variable it does not contain the value character 'y'.

it's the same as saying: int y;

what is y? it's not a character it's a number.
y = 666; // now is equal to 666.

using single letters for your variable names can become very confusing it's best to use words that make sense for example:

char yes='y', no='n';

then in the conditional if:

if ( next == yes || next == no ) // or something like this. it makes more sense...
Last edited on
in regards to indentation:

1
2
3
4
5
6
7
8
9
        if (next==Y||next==y)
	{
		cout<<"please enter your choice:";
		cin>>order;

	if (next==N||next==n)
	cout<<"your total is:"<<total<<"\t";
	
	}


The above block is not indented correctly: it should look like this:
1
2
3
4
5
6
7
8
9
10
        if (next==Y||next==y)
	{
		cout<<"please enter your choice:";
		cin>>order;

	        if (next==N||next==n)
	                cout<<"your total is:"<<total<<"\t";
	
	}


either that or you left out a opening curly brace & closing brace in which case it would look like this:

1
2
3
4
5
6
7
8
9
        if (next==Y||next==y)
	{
		cout<<"please enter your choice:";
		cin>>order;
        }
	if (next==N||next==n)
        {
	        cout<<"your total is:"<<total<<"\t";
	}

you have many many indentation problems through out this code, this was just 1 example.
Last edited on
Topic archived. No new replies allowed.