Why won't searchitem 2 work when complied?

#include <iostream>
#include <iomanip>
using namespace std;

int main ()

{
char search;
float searchitem;
string bookname;
float bookcopy;
float bookprice = 0;
char checkout;
string boardgame;
float boardgamecopy;
float cart = 0;
float subtotal = 0;

cout<<"You get an option to browse all categories or search our inventory.\n";

do{

cout<<"Do you want to search our inventory, y/n\n";
cin>>search;

if(search == 'y'){
cout<<"Let's start with a general description of what you would like to see.\n";
cout<<"Enter the number of the description.\n";
cout<<"1. Books\n";
cout<<"2. Board Games\n";
cout<<"3. Wizard Tower Clothing\n";
cout<<"4. Funko\n";
cin>>searchitem;
}

if(searchitem == 1){
cout<<"Please enter the name of the book.\n";
}

do{

cin>>bookname;

if(bookname == "D&D Fortune Cards: Fury Of The Feywild" || bookname == "D&D Fortune Cards: Neverwinter" || bookname == "D&D Fortune Cards: Shadow Over Nentir Vale"
|| bookname == "D&D Fortune Cards: Spiral Of Tharizdun"){
cout<<""<<bookname<<" cost CAD$39.99 each.n";
subtotal = subtotal + 39.99;
bookprice = bookprice + 39.99;
}

else if(bookname == "3.5 Edition Premium Spell Compendium" || bookname == "A Practical Guide To Monsters" || bookname == "AD&D 2nd Ed Dungeon Masters Guide"
|| bookname == "Demonomicon" || bookname == "Divine Power" || bookname == "Draconomicon Metallic Dragons" || bookname == "Dragon Magazine Annual"
|| bookname == "Dungeon Magazine Annual" || bookname == "E3 Prince Of Undead" || bookname == "Elminsters Forgotten Realms" || bookname == "Executioner Token Set"
|| bookname == "Martial Power 2" || bookname == "Players Strategy Guide" || bookname == "Power Cards: Warlord Power Cards" || bookname == "Psionic Powers"
|| bookname == "Revenge Of The Giants" || bookname == "The Plane Above Secrets Of The Astral Sea" || bookname == "The Plane Below Secrets Of The Elemental Chaos"
|| bookname == "Underdark"){
cout<<""<<bookname<<" cost CAD$29.99 each.\n";
subtotal = subtotal + 29.99;
bookprice = bookprice + 29.99;
}

else if(bookname != "3.5 Edition Premium Spell Compendium" && bookname != "A Practical Guide To Monsters" && bookname != "AD&D 2nd Ed Dungeon Masters Guide"
&& bookname != "D&D Fortune Cards: Fury Of The Feywild" && bookname != "D&D Fortune Cards: Neverwinter" && bookname != "D&D Fortune Cards: Shadow Over Nentir Vale"
&& bookname != "D&D Fortune Cards: Spiral Of Tharizdun" && bookname != "Demonomicon" && bookname != "Divine Power" && bookname != "Draconomicon Metallic Dragons"
&& bookname != "Dragon Magazine Annual" && bookname != "Dungeon Magazine Annual" && bookname != "E3 Prince Of Undead" && bookname != "Elminsters Forgotten Realms"
&& bookname != "Executioner Token Set" && bookname != "Martial Power 2"&& bookname != "Players Strategy Guide" && bookname != "Power Cards: Warlord Power Cards"
&& bookname != "Psionic Powers" && bookname != "Revenge Of The Giants" && bookname != "The Plane Above Secrets Of The Astral Sea"
&& bookname != "The Plane Below Secrets Of The Elemental Chaos" && bookname != "Underdark"){
cout<<"Unable to match your search with our inventory. Please try again with other keywords, check if cap locks is on.\n";
}

}while(bookname != "3.5 Edition Premium Spell Compendium" && bookname != "A Practical Guide To Monsters" && bookname != "AD&D 2nd Ed Dungeon Masters Guide"
&& bookname != "D&D Fortune Cards: Fury Of The Feywild" && bookname != "D&D Fortune Cards: Neverwinter" && bookname != "D&D Fortune Cards: Shadow Over Nentir Vale"
&& bookname != "D&D Fortune Cards: Spiral Of Tharizdun" && bookname != "Demonomicon" && bookname != "Divine Power" && bookname != "Draconomicon Metallic Dragons"
&& bookname != "Dragon Magazine Annual" && bookname != "Dungeon Magazine Annual" && bookname != "E3 Prince Of Undead" && bookname != "Elminsters Forgotten Realms"
&& bookname != "Executioner Token Set" && bookname != "Martial Power 2"&& bookname != "Players Strategy Guide" && bookname != "Power Cards: Warlord Power Cards"
&& bookname != "Psionic Powers" && bookname != "Revenge Of The Giants" && bookname != "The Plane Above Secrets Of The Astral Sea"
&& bookname != "The Plane Below Secrets Of The Elemental Chaos" && bookname != "Underdark");

do{

cout<<"How many copies would you like? You can buy up to five copies max.\n";
cin>>bookcopy;

if(bookcopy == 1 || bookcopy == 2 || bookcopy == 3 || bookcopy == 4 || bookcopy == 5){
cout<<""<<bookcopy<<" copy of it has been added to your cart.\n";
cart = cart + bookcopy;
}

if(bookcopy != 1 && bookcopy != 2 && bookcopy != 3 && bookcopy != 4 && bookcopy != 5){
cout<<"Amount not valid. Please try again.\n";
}

}while(bookcopy != 1 && bookcopy != 2 && bookcopy != 3 && bookcopy != 4 && bookcopy != 5);

if(searchitem == 2){
cout<<"Please enter the name of the board game.\n";
}

do{

cin>>boardgame;

if(boardgame == "A Game Of Thrones LCG 2ND ED. Calm Over Westeros" || boardgame == "A Game Of Thrones LCD 2ND ED. Ironborn Reavers Playmat"
|| boardgame == "A Game Of Thrones LCD 2ND ED. Kingslayer Playmat"){
cout<<""<<boardgame<<" cost CAD$31.42\n";
subtotal = subtotal + 31.42;
}

else if(boardgame == "40k Forbidden Stars" || boardgame == "504" || boardgame == "A Game of Thrones Board Game" || boardgame == "A Game Of Thrones LCG 2ND ED. Calm Over Westeros"
|| boardgame == "Aliens Vs Predators" || boardgame == "Empires Age Of Discovery W/EXP EGL" || boardgame == "Star Wars Imperial Assault" || boardgame == "Star Wars Armada"
|| boardgame == "Star Wars XMG Imperial Raider" || boardgame == "Ticket To Ride 10th Anniversary Edition" || boardgame == "Super Dungeon Explore Forgotten King"
|| boardgame == "Zombicide Expansion: Prison Outbreak" || boardgame == "Mega Man The Board Game" || boardgame == "Twilight Imperium 3RD Edition" || boardgame == "Rum and Bones Core Set"
|| boardgame == "Eclipse" || boardgame == "Zombicide Season 3: Rue Morgue" || boardgame == "Myth" || boardgame == "Iron Kingdoms Undercity" || boardgame == "Caverna: The Cave Farmers"
|| boardgame == "Space Cadets Away Missions"){
cout<<""<<boardgame<<" cost CAD$130.63\n";
subtotal = subtotal + 130.63;
}

else if(boardgame != "40k Forbidden Stars" && boardgame != "504" && boardgame != "A Game of Thrones Board Game" && boardgame != "A Game Of Thrones LCG 2ND ED. Calm Over Westeros"
&& boardgame != "A Game Of Thrones LCD 2ND ED. Ironborn Reavers Playmat" && boardgame != "A Game Of Thrones LCD 2ND ED. Kingslayer Playmat" && boardgame != "Aliens Vs Predators"
&& boardgame != "Empires Age Of Discovery W/EXP EGL" && boardgame != "Star Wars Imperial Assault" && boardgame != "Star Wars Armada" && boardgame != "Star Wars XMG Imperial Raider"
&& boardgame != "Ticket To Ride 10th Anniversary Edition" && boardgame != "Super Dungeon Explore Forgotten King" && boardgame != "Zombicide Expansion: Prison Outbreak"
&& boardgame != "Mega Man The Board Game" && boardgame != "Twilight Imperium 3RD Edition" && boardgame != "Rum and Bones Core Set" && boardgame != "Eclipse"
&& boardgame != "Zombicide Season 3: Rue Morgue" && boardgame != "Myth" && boardgame != "Iron Kingdoms Undercity" && boardgame != "Caverna: The Cave Farmers"
&& boardgame != "Space Cadets Away Missions"){
cout<<"Unable to match your search with our inventory. Please try again with other keywords, check if cap locks is on.\n";
}

}while (boardgame != "40k Forbidden Stars" && boardgame != "504" && boardgame != "A Game of Thrones Board Game" && boardgame != "A Game Of Thrones LCG 2ND ED. Calm Over Westeros"
&& boardgame != "A Game Of Thrones LCD 2ND ED. Ironborn Reavers Playmat" && boardgame != "A Game Of Thrones LCD 2ND ED. Kingslayer Playmat" && boardgame != "Aliens Vs Predators"
&& boardgame != "Empires Age Of Discovery W/EXP EGL" && boardgame != "Star Wars Imperial Assault" && boardgame != "Star Wars Armada" && boardgame != "Star Wars XMG Imperial Raider"
&& boardgame != "Ticket To Ride 10th Anniversary Edition" && boardgame != "Super Dungeon Explore Forgotten King" && boardgame != "Zombicide Expansion: Prison Outbreak"
&& boardgame != "Mega Man The Board Game" && boardgame != "Twilight Imperium 3RD Edition" && boardgame != "Rum and Bones Core Set" && boardgame != "Eclipse"
&& boardgame != "Zombicide Season 3: Rue Morgue" && boardgame != "Myth" && boardgame != "Iron Kingdoms Undercity" && boardgame != "Caverna: The Cave Farmers"
&& boardgame != "Space Cadets Away Missions");

do{

cout<<"How many copies would you like? You can buy up to two copies max.\n";
cin>>boardgamecopy;

if(boardgamecopy == 1 || boardgamecopy == 2){
cout<<""<<boardgamecopy<<" copy of it has been added to your cart.\n";
cart = cart + boardgamecopy;
}

else if(boardgamecopy != 1 && boardgamecopy != 2){
cout<<"Amount not valid. Please try again.\n";
}

}while(boardgamecopy != 1 && boardgamecopy != 2);

cout<<"Do you want to check out? y/n\n";
cin>>checkout;

}
Last edited on
Please, use code tags (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your
post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <>
formatting button.
It just gives me an error when I tried it.
Your code is hard to read to troubleshoot it without the code tags.

Please, use code tags (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your
post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <>
formatting button.
First off you have compile errors.

You have mismatched {}. The code is such a mess to read, I didn't bother trying to find the missing }.

Line 3: You're missing <string> header.

Your code is hard to read without code tags and decent indentation.

Your code would also be easier to follow if you used functions. e.g.
1
2
3
4
    if(searchitem == 1)
        subtotal += buy_book ();
    if (searchitem == 2)
        subtotal += buy_boardgame (); 


You're going to have a problem with your cin statements. cin stops at the first whitespace. Your book and board game titles contains spaces. Use getline.

Use double instead of float to avoid truncation warnings.

As to your question, searchitem is a float. floats are approximations. Change searchitem to an int.
Last edited on
Here's a cleaned up version of your code that is far easier to read:

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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#include <iostream>
#include <iomanip> 
#include <string>
#include <map>
using namespace std;

map<string,double>  books;
map<string,double>  games;
        
void load_book (const string & bookname, double price)
{   books.insert (pair<string,double> (bookname, price));
}

void load_game (const string & gamename, double price)
{   games.insert (pair<string,double> (gamename, price));
}

void load_book_inventory ()
{   load_book ("D&D Fortune Cards: Fury Of The Feywild", 39.99);
    load_book ("D&D Fortune Cards: Neverwinter", 39.99); 
    load_book ("D&D Fortune Cards: Shadow Over Nentir Vale", 39.99); 
    load_book ("D&D Fortune Cards: Spiral Of Tharizdun", 39.99); 
    load_book ("3.5 Edition Premium Spell Compendium", 29.99);
    load_book ("A Practical Guide To Monsters", 29.99); 
    load_book ("AD&D 2nd Ed Dungeon Masters Guide", 29.99);
    load_book ("Demonomicon", 29.99);
    load_book ("Divine Power", 29.99); 
    load_book ("Draconomicon Metallic Dragons", 29.99);
    load_book ("Dragon Magazine Annual", 29.99);
    load_book ("E3 Prince Of Undead", 29.99); 
    load_book ("Elminsters Forgotten Realms", 29.99); 
    load_book ("Executioner Token Set", 29.99);
    load_book ("Martial Power 2", 29.99); 
    load_book ("Players Strategy Guide", 29.99);
    load_book ("Power Cards: Warlord Power Cards", 29.99);
    load_book ("Psionic Powers", 29.99); 
    load_book ("Revenge Of The Giants", 29.99);
    load_book ("The Plane Above Secrets Of The Astral Sea", 29.99);
    load_book ("The Plane Below Secrets Of The Elemental Chaos", 29.99);
    load_book ("Underdark", 29.99); 
}

double buy_book ()
{   string bookname;
    double bookprice;    
    int copies;
    map<string,double>::const_iterator  citer;
    
    cout << "Enter book name to search for: ";
    getline (cin, bookname); 
    citer = books.find (bookname);
    if (citer == books.end())
    {   cout << "Book name not found" << endl;
        return 0;
    }
    bookprice = citer->second;
    cout << "That book is CDN$ " << bookprice << endl;
    do
    {   cout << "How many copies would you like? You can buy up to five copies max." << endl;
        cin >> copies;
        if (copies >= 1 && copies <= 5) 
        {   cout << copies <<" copies of it havae been added to your cart.\n";
        }
        else
        {   cout << "Copies not valid. Please try again.\n";
        }
    } while (copies < 1 || copies > 5);
    bookprice = citer->second * copies;
    return bookprice;
}

void load_game_inventory ()
{   load_game ("A Game Of Thrones LCG 2ND ED. Calm Over Westeros", 31.42);
    load_game ("A Game Of Thrones LCD 2ND ED. Ironborn Reavers Playmat", 31.42);
    load_game ("A Game Of Thrones LCD 2ND ED. Kingslayer Playmat", 31.42); 
    load_game ("40k Forbidden Stars", 130.63);
    load_game ("504", 130.62);
    load_game ("A Game of Thrones Board Game", 130.63);
    load_game ("A Game Of Thrones LCG 2ND ED. Calm Over Westeros", 130.63);  // Duplicate ???
    load_game ("Aliens Vs Predators", 130.63);
    load_game ("Empires Age Of Discovery W/EXP EGL", 130.63);
    load_game ("Star Wars Imperial Assault", 130.63);
    load_game ("Star Wars Armada", 130.63);
    load_game ("Star Wars XMG Imperial Raider", 130.63);
    load_game ("Ticket To Ride 10th Anniversary Edition", 130.63);
    load_game ("Super Dungeon Explore Forgotten King", 130.63); 
    load_game ("Zombicide Expansion: Prison Outbreak", 130.63);
    load_game ("Mega Man The Board Game", 130.63);
    load_game ("Twilight Imperium 3RD Edition", 130.63);
    load_game ("Rum and Bones Core Set", 130.63);
    load_game ("Eclipse", 130.63);
    load_game ("Zombicide Season 3: Rue Morgue", 130.63);
    load_game ("Myth", 130.63);
    load_game ("Iron Kingdoms Undercity", 130.63);
    load_game ("Caverna: The Cave Farmers", 130.63);
    load_game ("Space Cadets Away Missions", 130.63);
}    

double buy_game ()
{   string boardgame;
    map<string,double>::const_iterator citer;
    double gameprice;
    int     copies;
    
    cout<<"Please enter the name of the board game.\n";
    getline (cin, boardgame);
    citer = games.find (boardgame);
    if (citer == games.end())
    {   //  Not found
        cout << "Board game not found" << endl;
        return 0;
    }
    gameprice = citer->second;    
    cout << "That game is CDN$ " << gameprice << endl;
    while (true)
    {   cout << "How many copies would you like? You can buy up to two copies max." << endl;
        cin >> copies; 
        if (copies == 1 || copies == 2)
        {   cout << copies << " copies of it have been added to your cart." << endl;;
            return gameprice * copies; 
        }
        cout << "Copies not valid. Please try again.\n";
    } 
}
    
int main ()
{   char search;
    int searchitem;
    char checkout;
    double subtotal = 0;

    load_book_inventory ();
    load_game_inventory ();
    cout<<"You get an option to browse all categories or search our inventory.\n";
    do
    {   cout<<"Do you want to search our inventory, y/n\n";
        cin>>search;
        if (search == 'y')
        {   cout<<"Let's start with a general description of what you would like to see.\n";
            cout<<"Enter the number of the description.\n";
            cout<<"1. Books\n";
            cout<<"2. Board Games\n";
            cout<<"3. Wizard Tower Clothing\n";
            cout<<"4. Funko\n";
            cin>>searchitem;        
            if (searchitem == 1)
                subtotal += buy_book ();
            if(searchitem == 2)
                subtotal += buy_game ();
        }
    } while (search = 'y');
    cout<<"Do you want to check out? y/n\n";
    cin>>checkout;
}

Topic archived. No new replies allowed.