So, I have done projects with all of the following, and now it is my time to compile them all together... And I can't seem to do it... All I'm really looking for is a skeleton because I can't figure it out... I have no background knowledge in C++ and am a shrimp among sharks in my class...
I am looking to first have the user input 5 numbers by creating an array as a global class as the first function. This will then lead to me move forward to the second function, opening up a menu by asking the user to view these numbers in either Ascending or Descending orders. Each of these options will then create a third and fourth function, respectively.
I am also looking to take this one step at a time as I work along with new revelations...
#include <iostream>
usingnamespace std;
#include "header.h"
#include <cstdlib>
#include <cstring>
int main()
{
while (1)
{
char choice[2];
int num1 = 1;
int num2 = 2;
int up;
cout << "::Welcome to my sorting program\n\n"
<< "Please choose an option to view my work\n\n"
<< "1. Ascending Order \n"
<< "2. Descending Order \n"
<< "3. Ascending and Descending Order \n"
<< "4. Exit \n\n"
<< "Choice: ";
cin >> choice;
if (choice[0] == '1')
{
{
{ {
return 0;
}
}
}
system("CLS");
cout << "Ascending\n\n";
system("PAUSE");
system("CLS");
}
elseif (choice[0] == '2')
{
{
{
return 0;
}
}
system("CLS");
cout << "Descending Order\n\n";
system("PAUSE");
system("CLS");
}
elseif (choice[0] == '3')
{
{
}
return 0;
}
}
system("CLS");
cout << "Ascending and Descending Order\n\n";
system("PAUSE");
system("CLS");
}
elseif (choice[0] == '4')
{
{
return 0;
}
elseif (choice[0] > '4' || choice[0] < '1')
{
system("CLS");
cout << "Invalid Input\n\n";
system("PAUSE");
system("CLS");
}
else
{
system("CLS");
cout << "Option 1\n\n";
system("PAUSE");
system("CLS");
}
}
system("CLS");
cout << "Ascending Order\n\n";
system("PAUSE");
system("CLS");
}
}
//int numbers[10];
for (int i = 0 , j = 0; i < 10; i++)
{
cout << " Please enter number " << i << endl;
cin >> numbers[i];
}
i = 0;
int greater = 0;
for (; i<10; i++)
{
if (numbers[i] >= 10)
greater++;
cout << "number of elements greater or equal to 10 is: " << greater << endl;//stuff I found on yahoo answers
return 0;
}
Some stuff I found on yahoo answers to maybe solve my arrays issue... I looked and saw that this may be more difficult than it needs to be and saw that I can instead use a Bubble Sort.
Next issue is not being able to file these functions and be able to have function 1 (The Array) call Function 2 (The Menu) which then can call Function 3, 4, 5 (Ascending, Descending, and Ascending and Descending options)
#include<iostream>
#include<string>
usingnamespace std;
int main(char a, char b, char c)
{
int array[5];
cout << "Enter 5 numbers randomly : " << endl; //Ask User To Enter 5 numbers
for (int i = 0; i<5; i++)
{
cin >> array[i]; // Input for entering 5 numbers
}
cout << endl;
cout << "a) Increasing Order !!.\n" // Displaying Menu for user to choose between 3 choices
"b) Decreasing Order !!.\n""c) Both Orders !!.\n" << endl;
char choice;
cout << "enter a, b, or c: " << endl; // Asking user to choose one of the choices
cin >> choice; // input for user to choose
switch (choice)
{
if (choice = a)
{ //if user enters choice a
case'a': for (int L = 0; L <= 4; L++)
{
for (int j = 0; j < 4; j++)
{
if (array[j] > array[j + 1])
{
int temp1 = array[j];
array[j] = array[j + 1];
array[j + 1] = temp1;
}
}
}
cout << "*******Increasing is: " << endl;
for (int m = 0; m < 5; m++)
{
cout << array[m] << endl; // Displaying elements by increasing order
}
break; //Program stops
}
if (choice = b) // if user enters choice b
{
case'b':for (int k = 0; k <= 4; k++)
{
for (int j = 0; j < 4; j++)
{
if (array[j] < array[j + 1])
{
int temp2 = array[j];
array[j] = array[j + 1];
array[j + 1] = temp2;
}
}
}
cout << "*******Decreasing is: " << endl;
for (int m = 0; m < 5; m++)
{
cout << array[m] << endl; // displaying elements by decreasing order
}
break; //Program stops
}
if (choice = c) // if user chooces c
case'c':
{
break; //Program stops
}
default: //if user does not choose one of the above choices
{cout << "You Entered Wrong Choice !!!!!" << endl; //display message user entered wrong
break;
}
}
return 0;
}
Okay, this is what I worked on so far....
I'm not really able to have it work because it is still missing a header file.
Firstly, I'd strongly recommend you adopt a sensible, consistent indentation style. It would make it much easier for both you and us to actually read your code, and see the flow of control and logic.
Why is your main function defined as taking 3 char arguments? That's not legal. What are you trying to achieve there?
You don't seem to know the difference between the assignment operator = and the equality comparison operator ==. I suggest you learn that, and fix your code so that your comparisons do what you want them to do.
Your switch-case statement seems to be trying to use an odd mix of cases and if statements that don't make a lot of sense. I suspect you need to go back and re-read the section on switch-case, and learn how you really write them.
#include<iostream>
#include<string>
usingnamespace std;
int main()
{
int array[5];
cout << "Enter 5 numbers randomly : " << endl; //Ask User To Enter 5 numbers
for (int i = 0; i<5; i++)
{
cin >> array[i]; // Input for entering 5 numbers
}
cout << endl;
cout << "a) Increasing Order !!.\n" // Displaying Menu for user to choose between 3 choices
"b) Decreasing Order !!.\n""c) Both Orders !!.\n" << endl;
char choice;
cout << "enter a, b, or c: " << endl; // Asking user to choose one of the choices
cin >> choice; // input for user to choose
switch (choice)
{ //if user enters choice a
case'a':
for (int L = 0; L <= 4; L++)
{
for (int j = 0; j < 4; j++)
{
if (array[j] > array[j + 1])
{
int temp1 = array[j];
array[j] = array[j + 1];
array[j + 1] = temp1;
}
}
}
cout << "*******Increasing is: " << endl;
for (int m = 0; m < 5; m++)
{
cout << array[m] << endl; // Displaying elements by increasing order
}
break; //Program stops
case'b':
for (int k = 0; k <= 4; k++)
{
for (int j = 0; j < 4; j++)
{
if (array[j] < array[j + 1])
{
int temp2 = array[j];
array[j] = array[j + 1];
array[j + 1] = temp2;
}
}
}
cout << "*******Decreasing is: " << endl;
for (int m = 0; m < 5; m++)
{
cout << array[m] << endl; // displaying elements by decreasing order
}
break; //Program stops
case'c':
{
break; //Program stops
}
default: //if user does not choose one of the above choices
{
cout << "You Entered Wrong Choice !!!!!" << endl; //display message user entered wrong
}
break;
}
return 0;
}
This compiles and does part of what you want, option 'c' isn't finished (as you probably know). You had if statements in the middle of your switch statement! Removing those mostly fixed your problems. I would probably make separate functions to display them ascending/descending and call them in the appropriate spot in the switch statement.