Why wont this code run correctly?

When you select option 3, nothing happens. Can some one help me with this?

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
 #include <iostream>
#include <string>
using namespace std;




int linearSearch(int bookId[], int size, int searchValue)
{
    for (int i = 0; i < size; i++)
    {
        if (searchValue == bookId[i])
        {
            return i;
        }
    }
    return -1;
}






int main()

{



   int choice;

    cout << endl;
    cout << "Choose from the menu below: " << endl;
    cout << "1: Linear Search" << endl;
    cout << "2: Binary Search" << endl;
    cout << "3: Bubble Sort" << endl;
    cout << "4: Selection Sort" << endl;
    cout << "5: Quit" << endl;
    cin >> choice;


if (choice == 1) {
string bookTitle[10] = {"Starting Out With C++" ,"Java Programming",
"Software Structures" , "Deisgn and Analysis of Algorithms" , "Computer Graphics",
 "Artifical Inteligence: A Modern Approach" , " Probability and Statistics" , "Cognitive Science" ,
 "Modern Information Retrieval" , "Speech and Language Processing"};

int bookId[10] = {1101, 1211, 1333, 1456, 1567, 1642, 1699, 1755, 1800, 1999};

double bookPrice[10] = {112.32, 73.25, 54.00, 67.32, 135.00, 173.22, 120.00, 42.25, 32.11, 123.75};

cout << "Book ID" << "   " << "Book Name" << "             " << "Book Price" << endl;
    for(int x = 0; x < (10); x++)
{
	cout << bookId[x] << "   " << bookTitle[x] << "   " << "$" << bookPrice[x] << endl;
}

int userValue;

cout << "Enter in a book ID: " << endl;
cin >> userValue;

int result = linearSearch(bookId, 10, userValue);

if (result >= 0)
{
   cout << "The number " << bookId[result] << " was found" << endl;
}
else
{
    cout << "The Book ID " << userValue << " was not found" << endl;

}
if (choice == 3) {


int bubbleArray[1000];

cout << endl;
cout << "Unsorted Array: " << endl;

for (int a = 0; a<1000; a++) {
int a;
int j;
int tmp;
bubbleArray[a] = rand()%500;
cout<<bubbleArray[a]<<endl;
}
cout << endl;

cout << "Sorted Array: " << endl;
for (int a = 0; a<999; a++) {
for (int j = 0; j<999; j++) {
if (bubbleArray[j]>bubbleArray[j+1]) {
int tmp = bubbleArray[j];
bubbleArray[j] = bubbleArray[j+1];
bubbleArray[j+1] = tmp;
}
}
}

for (int a = 0; a<1000; a++) {
cout<<bubbleArray[a]<<endl;
}

}




}


return 0;

}
If you fix your indentation, you will see the problem clear as day. It is masked by your lack of indentation.

Hint: the choice cannot be 3 if it is already determined to be 1.
Last edited on
Ok figured that out, but why when I run it, it only says "unsorted array" in the output?
Please post your current code so I am seeing the same thing as you.
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;




int linearSearch(int bookId[], int size, int searchValue)
{
for (int i = 0; i < size; i++)
{
if (searchValue == bookId[i])
{
return i;
}
}
return -1;
}






int main()

{



int choice;

cout << endl;
cout << "Choose from the menu below: " << endl;
cout << "1: Linear Search" << endl;
cout << "2: Binary Search" << endl;
cout << "3: Bubble Sort" << endl;
cout << "4: Selection Sort" << endl;
cout << "5: Quit" << endl;
cin >> choice;


if (choice == 1) {
string bookTitle[10] = {"Starting Out With C++" ,"Java Programming",
"Software Structures" , "Deisgn and Analysis of Algorithms" , "Computer Graphics",
"Artifical Inteligence: A Modern Approach" , " Probability and Statistics" , "Cognitive Science" ,
"Modern Information Retrieval" , "Speech and Language Processing"};

int bookId[10] = {1101, 1211, 1333, 1456, 1567, 1642, 1699, 1755, 1800, 1999};

double bookPrice[10] = {112.32, 73.25, 54.00, 67.32, 135.00, 173.22, 120.00, 42.25, 32.11, 123.75};

cout << "Book ID" << " " << "Book Name" << " " << "Book Price" << endl;
for(int x = 0; x < (10); x++)
{
cout << bookId[x] << " " << bookTitle[x] << " " << "$" << bookPrice[x] << endl;
}

int userValue;

cout << "Enter in a book ID: " << endl;
cin >> userValue;

int result = linearSearch(bookId, 10, userValue);

if (result >= 0)
{
cout << "The number " << bookId[result] << " was found" << endl;
}
else
{
cout << "The Book ID " << userValue << " was not found" << endl;

}
}
if (choice == 3) {
int bubbleArray[1000];

for (int a = 0; a<1000; a++) {
int a;
int j;
int tmp;
bubbleArray[a] = rand()%500;
cout<<bubbleArray[a]<<endl;
}
cout << endl;

cout << "Sorted Array: " << endl;
for (int a = 0; a<999; a++) {
for (int j = 0; j<999; j++) {
if (bubbleArray[j]>bubbleArray[j+1]) {
int tmp = bubbleArray[j];
bubbleArray[j] = bubbleArray[j+1];
bubbleArray[j+1] = tmp;
}
}
}


for (int a = 0; a<1000; a++) {
cout<<bubbleArray[a]<<endl;
}

}







return 0;

}
Please edit your post and make sure your code is [code]between code tags[/code] so that it has syntax highlighting and line numbers, as well as proper indentation.

You are redeclaring a inside your for loop. Don't do that.
Topic archived. No new replies allowed.