Need help with code

I have this code and it keeps giving me these errors.

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

using namespace std;

int main()
{
    int money = 0;
    int inventory[7] = {0, 1, 2, 3, 4, 5, 6};
    string BNAME; //Business name
    string PNAME; //Player name
    string choice;

    cout << "New" << endl;
    cout << "Load" << endl;
    cout << "\n";
    cin >> choice;

    if(choice == "New" || choice == "new")
    {
        goto NEW;
    }

    if(choice == "Load" || choice == "load")
    {
        goto LOAD;
    }

    NEW:

    cout << "please enter your business name." << endl;
    cin >> BNAME;
    cout << "\n";
    cout << "Now please enter your name." << endl;
    cin >> PNAME;
    cout << "\n";

    LOAD:

}



ERRORS:


C:\Users\Chay Hawk\Desktop\test\main.cpp||In function 'int main()':|
C:\Users\Chay Hawk\Desktop\test\main.cpp|40|error: expected primary-expression before '}' token|
C:\Users\Chay Hawk\Desktop\test\main.cpp|40|error: expected ';' before '}' token|
C:\Users\Chay Hawk\Desktop\test\main.cpp|10|warning: unused variable 'money'|
C:\Users\Chay Hawk\Desktop\test\main.cpp|11|warning: unused variable 'inventory'|
||=== Build finished: 2 errors, 2 warnings ===|


I have all my ;'s placed why does it keep saying that it needs one before }?
gotos :D

There is no statement at LOAD.

Could you do without them?
i got it working :P why not use them? i mean i know they can cause problems and all that but if they are apart of C++ then why not? also have another problem, ifstream doesnt work

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

using namespace std;

int main()
{
    int money = 0;
    int item[7] = {0, 1, 2, 3, 4, 5, 6};
    string BNAME; //Business name
    string PNAME; //Player name
    string choice;

    ofstream file("FILE.txt");

    cout << "New" << endl;
    cout << "Load" << endl;
    cout << "\n";
    cin >> choice;

    if(choice == "New" || choice == "new")
    {
        goto NEW;
    }

    if(choice == "Load" || choice == "load")
    {
        goto LOAD;
    }

    NEW:

    cout << "please enter your business name." << endl;
    cin >> BNAME;
    cout << "\n";
    cout << "Now please enter your name." << endl;
    cin >> PNAME;
    cout << "\n";
    cout << "Now you will be asked to enter several products you" << endl;
    cout << "wish to sell in your shop, enter the name and press enter" << endl;
    cout << "\n";
    cout << "Item 1: "; cin >> item[0];
    cin.get();
    cout << "Item 2: "; cin >> item[1];
    cin.get();
    cout << "Item 3: "; cin >> item[2];
    cin.get();

    file << BNAME << endl;
    file << PNAME << endl;

    LOAD:

    file >> BNAME;
    file >> PNAME;

    cout << BNAME << endl;

}



C:\Users\Chay Hawk\Desktop\test\main.cpp||In function 'int main()':|
C:\Users\Chay Hawk\Desktop\test\main.cpp|52|error: no match for 'operator>>' in 'file >> BNAME'|
C:\Users\Chay Hawk\Desktop\test\main.cpp|53|error: no match for 'operator>>' in 'file >> PNAME'|
C:\Users\Chay Hawk\Desktop\test\main.cpp|11|warning: unused variable 'money'|
||=== Build finished: 2 errors, 1 warnings ===|
Last edited on
if they are apart of C++ then why not?


Because they will be used to write spaghetti code that is difficult to read, hard to understand, and problematic to maintain.

What you've done here is use goto to very clumsily implement crippled, dangerous versions of something we we already have in a far superior format; functions. Do you recognise that when you pick NEW, when that section is finished, the section marked LOAD will also be executed, even though you didn't pick LOAD?

also have another problem, ifstream doesnt work

And which ifstream would that be? Your code doesn't have any.
file >> BNAME; file is an object of type ofstream. For outputting. You're trying to read in from an output.
Last edited on
Ok so I guess I wont use goto then, but I did have an ifstream in my file and it still didnt work, so what do I do.
I did have an ifstream in my file and it still didnt work,


You need to find the error in your code and fix it.
I dont know how to fix the error though. If i knew how to fix the error i wouldnt ask for help.

Here is how to use ifstream to open a file, read something, and close the file.

1
2
3
4
string aString;
ifstream input("filename");
input >> aString;
input.close();
i know how to use fstream but never mind im not using gotos anymore, but i have another problem, i need the player to be able to enter a bunch of different products that they want to sell but it just skips right over them. I get no compiler errors it just doesnt work the way i want. I did try using getline but it doesnt seem to work with array elements. i also had cin.get(); and the end of each input but that didnt work either. if you run the code you'll see what i mean.

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

using namespace std;

void MAINPROGRAM();

int main()
{
    int money = 0;
    int item[7] = {0, 1, 2, 3, 4, 5, 6};
    string BNAME; //Business name
    string PNAME; //Player name
    string choice;

    ofstream file("FILE.txt");

    cout << "New" << endl;
    cout << "Load" << endl;
    cout << "\n";
    cin >> choice;

    if(choice == "New" || choice == "new")
    {
        cout << "please enter your business name." << endl;
        getline(cin, BNAME);
        cout << "\n";
        cin.get();
        cout << "Now please enter your name." << endl;
        getline(cin, PNAME);
        cout << "\n";
        cin.get();
        cout << "Now you will be asked to enter several products you" << endl;
        cout << "wish to sell in your shop, enter the name and press enter" << endl;
        cout << "\n";
        cout << "Item 1: "; cin >> item[0];
        cout << "\n";
        cout << "Item 2: "; cin >> item[1];
        cout << "\n";
        cout << "Item 3: "; cin >> item[2];
        cout << "\n";
        cout << "Item 4: "; cin >> item[3];
        cout << "\n";
    }

    if(choice == "Load" || choice == "load")
    {
        MAINPROGRAM();
    }
}

void MAINPROGRAM()
{

}
Last edited on

Here is the output I got. It did not skip over anything. I was able to enter a number for each item.

j@j-desktop:~/badCode$ ./a.out 
New
Load

New
please enter your business name.

a
Now please enter your name.

b
Now you will be asked to enter several products you
wish to sell in your shop, enter the name and press enter

Item 1: 3

Item 2: 4

Item 3: 5

Item 4: 6


You are entering a number, aren't you? Since the values are being stored in an array of int values, I guessed that you wanted to store integers.
Last edited on
No i wanted the person to enter a name for the product, i tried making a string array but it wouldt let me. but there will be numbers associated with the array elements like how many are in stock etc. so i wasnt sure.

so item 1 - football = 10
Last edited on
You are storing the user input in the array named item. item is an array of int values. An int is a single number. You can store single numbers in an int. You cannot store strings in an int.

If you want to store a string, use a string. For example

1
2
3
4
5
6
7
8
string item[7];
cout << "Item 1: "; cin >> item[0];
cout << "\n";
cout << "Item 2: "; cin >> item[1];
cout << "\n";
cout << "Item 3: "; cin >> item[2];
cout << "\n";
cout << "Item 4: "; cin >> item[3];
Last edited on
oh ok, that confused me but i get it now.
Last edited on
i have yet another problem, i noticed that when entering the players name, if i press enter 2 times it skips item name 1 and 2 when i press it 3 times it skips 1 2 and 3, why is it doing this? when i just enter one line it doesnt skip any.
bump
Topic archived. No new replies allowed.