Hi, I want to develop a code where the user has the decision to create an account or continue as a guest, but the "if" nerves get executed. I didn't continue the code because I couldn't pass this step, but to give more context, my idea is to create a shop menu. Also, I don't know if I'm complicating the code with the structs, but I just one to simulate an online store type.
[code]
#include <iostream>
#include <iomanip>
using namespace std;
cout << endl << endl;
cout << "\t****************************" << endl;
cout << "\t Welcome To Shop" << endl;
cout << "\t****************************" << endl;
cout << "\n \t Do you wnat to create an account before your pucharses?" << endl;
cout << "\t Type (Y/N)" << endl;
cout << "\t If N you will continue as a guest" << endl;
You've declared choice as an int. Make it a char instead.
I see you tried to use code tags, but it didn't work, please add [/code] to the end of your post. In the future, you can tag bits of code by highlighting them, and then clicking the "<>" button to the right of the edit window.
The struct will probably make the code easier, but it's too early to say for sure.
#include <iostream>
usingnamespace std;
void account();
struct Account{
char first[15];
char middle[15];
char last[15];
char address_1[30];
char address_2[30];
char city[15];
char state[15];
char email[30];
};
int main()
{
account();
return 0;
}
void account() {
Account r;
char fst[15];
cout << endl << endl;
cout << "\t****************************" << endl;
cout << "\t Welcome To Shop" << endl;
cout << "\t****************************" << endl;
cout << "\n \t Do you wnat to create an account before your pucharses?" << endl;
cout << "\t Type (Y/N)" << endl;
cout << "\t If N you will continue as a guest" << endl;
char choice;
cin >> choice;
if (choice == 'Y') {
cout << "Account Details" << endl << "Enter First Name:" << endl;
cin.ignore(1000, '\n'); // <---
cin.getline(r.first,15);
cout << "First name is " << r.first << '\n';
}
else (choice == 'N');
}
****************************
Welcome To Shop
****************************
Do you wnat to create an account before your pucharses?
Type (Y/N)
If N you will continue as a guest
Y
Account Details
Enter First Name:
Robert Smith
First name is Robert Smith
Program ended with exit code: 0
In the future, you can tag bits of code by highlighting them, and then clicking the "<>" button to the right of the edit window.
There is a bug that prevents this when creating a new topic. You either will have to add the tags manually, or submit the topic and then click edit which will allow you to use the format buttons.