If...Else Statement doesn't executes

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;

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;

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;

int choice;
cin >> choice;

if (choice == 'Y') {
cout << "Account Details" << endl << "Enter First Name:" << endl;
cin.getline(r.first, 15);
}
else (choice == 'N');

}
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.
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
#include <iostream>

using namespace 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
dhayden wrote:
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.
Last edited on
Topic archived. No new replies allowed.