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