alternative to if statement
Oct 26, 2017 at 8:18pm UTC
My code needs help because there is no way to get to the second if statement because the first if statement needs to be one first. The program need some work to make use of both if statements.
Are there any alternatives rather than an if statement that I can use so both statements can work when the user inputs their choice.
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
#include <iostream>
#include<cstdlib>
#include<ctime>
double getAverage(int [],int );
using namespace std;
int main()
{
int input;
cout << "Enter your menu choice" << endl;
cout << "0. Create Array" << endl;
cout << "1. Find high, low, sum average" << endl;
cout << "2. Add a number to the end" << endl;
cout << "3. Find the index of a number" << endl;
cout << "4. Insert number at index" << endl;
cout << "5. Remove number" << endl;
cout << "6. Remove number at index" << endl;
cout << "7. Quit" << endl;
cin >> input;
const int LENGTH =100;
int array[LENGTH];
srand(((unsigned )time(0)));
if (input==0){
int cnt = 0 ;
cout << "Enter the length of the array: " ;
cin >> cnt ;
if ( cnt > LENGTH ) cnt = LENGTH ;
for ( int i = 0; i < cnt ; ++i )
array[i] = 1 + rand() % 100;
for ( int i = 0; i < cnt ; ++i )
cout << array[i] << ' ' ;
cout << '\n' ;
if (input == 1){
int sum = 0;
int average = 0;
int high = array[0];
int low = array[0];
for (int dx = 0; dx < cnt; dx++)
{
sum += array[dx];
if (array[dx] > high) high = array[dx];
if (array[dx] < low) low = array[dx];
}
average = sum/ cnt;
cout << high << " " << low << " " << average << endl;}
}
return 0;
}
Last edited on Oct 26, 2017 at 8:20pm UTC
Oct 26, 2017 at 8:56pm UTC
You have a few problems, one of them being misplaced curly braces... your block style and indentation are not uniform.
Look at the braces placement on lines 46 and 49. What do they match up with?
Notice the difference in logic:
1 2 3 4 5 6 7
if (a)
{
if (b) // will only run if b is true AND a is true
{
}
}
1 2 3 4 5 6 7 8
if (a)
{
}
if (b) // does not depend on a being true
{
}
However, your second problem is that (1-6) logically depends on (0) because you use the cnt and array variables in (1-6) after initially populating them in (0).
I think you should make it so (0) always happens, and then make the options 1-7 instead of 0-7.
Is this better for you?
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
#include <iostream>
#include<cstdlib>
#include<ctime>
double getAverage(int [],int );
using namespace std;
int main()
{
const int LENGTH =100;
int array[LENGTH];
srand(((unsigned )time(0)));
int cnt = 0;
cout << "Enter the length of the array: " ;
cin >> cnt ;
if ( cnt > LENGTH )
cnt = LENGTH ;
for ( int i = 0; i < cnt ; ++i )
array[i] = 1 + rand() % 100;
for ( int i = 0; i < cnt ; ++i )
cout << array[i] << ' ' ;
cout << '\n' ;
int input;
cout << "Enter your menu choice" << endl;
cout << "1. Find high, low, sum average" << endl;
cout << "2. Add a number to the end" << endl;
cout << "3. Find the index of a number" << endl;
cout << "4. Insert number at index" << endl;
cout << "5. Remove number" << endl;
cout << "6. Remove number at index" << endl;
cout << "7. Quit" << endl;
cin >> input;
if (input == 1) {
int sum = 0;
int average = 0;
int high = array[0];
int low = array[0];
for (int dx = 0; dx < cnt; dx++)
{
sum += array[dx];
if (array[dx] > high) high = array[dx];
if (array[dx] < low) low = array[dx];
}
average = sum/ cnt;
cout << high << " " << low << " " << average << endl;
}
else if (input == 2)
{
cout << "Not implemented!" << endl;
}
return 0;
}
You also will probably want to create a loop around the choice logic, so that you can do more than one thing per program run.
Last edited on Oct 26, 2017 at 8:59pm UTC
Oct 26, 2017 at 9:07pm UTC
thanks, I think your way works better.
Topic archived. No new replies allowed.