7 Replies - 322 Views - Last Post: 23 March 2013 - 06:40 AM Rate Topic: -----

#1 HanJiyoon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 22-March 13

Switch (select) error

Posted 22 March 2013 - 08:41 PM

I looked over the internet in some codes but it has the same format at all.
Close tag "}" and then switch (select) then open bracket "{".
Did I forgot something to put on my program ?

Posted Image

Thank you. :)/>/>/>/>/>

Sorry for double posting :X
The highlighted one is the error.
#include <stdio.h>
#include <conio.h>
#include <iostream>
#include <malloc.h>
using namespace std;
int select;
int main()
{
cout<<"Choose your Sorting Algorithm: \n";
cout<<"[1] BubbleSort \n";
cout<<"[2] InsertionSort \n";
cout<<"[3] MergeSort \n";
cout<<"[4] QuickSort \n";
cout<<"Select [1-4] \n";
cin>> select;
}
//Switch
switch (select)
{
case 1:goto BubbleSort; 
case 2:goto InsertionSort;
case 3:goto MergeSort;
case 4:goto QuickSort;
default: cout<<"Invalid Choice!";
goto exit;

BubbleSort:
{
int array[100], n, c, d, swap;

cout>>"Enter number of elements\n";
cout>>"Enter integers\n", n;

for (c = 0; c < n; c++)
cin>>&array[c]);

for (c = 0 ; c < ( n - 1 ); c++)
{
for (d = 0 ; d < n - c - 1; d++)
{
if (array[d] > array[d+1]) /* For decreasing order use < */
{
swap = array[d];
array[d] = array[d+1];
array[d+1] = swap;
}
}
}

cout>>"Sorted list in ascending order:\n";

for ( c = 0 ; c < n ; c++ )
cout>>"%d\n", array[c];

}//Select Case
 break;
InsertionSort: 
{
{
int n, array[1000], c, d, t;

printf("Enter number of elements\n");
scanf("%d", &n);

printf("Enter %d integers\n", n);

for (c = 0; c < n; c++) {
scanf("%d", &array[c]);
}

for (c = 1 ; c <= n - 1; c++) {
d = c;

while ( d > 0 && array[d] < array[d-1]) {
t = array[d];
array[d] = array[d-1];
array[d-1] = t;

d--;
}
}

printf("Sorted list in ascending order:\n");

for (c = 0; c <= n - 1; c++) {
printf("%d\n", array[c]);
 break;
}
}//Select Case
MergeSort:
void mergesort(int[],int,int);
void merge(int[],int,int,int);
int main()
{
int a[10],p,q,r,i,n;
cout<<"Enter the number of elements";
cin>>n;
p=0;
r=n-1;
cout<<"Enter the array";
for(i=0;i<n;i++)
{
cin>>a[i];
}
mergesort(a,p,r);
cout<<"The sorted array is:";
for(i=0;i<n;i++)
{
cout<<"\n"<<a[i];
}
getch();
}
void mergesort(int a[],int p,int r)
{
if( p < r)
{
int q=(p+r)/2;
mergesort(a,p,q);
mergesort(a,q+1,r) ;
merge(a,p,q,r);
}
}
void merge(int a[],int p,int q,int r)
{
int c[10];
int i=p;
int j=q+1;
int k=p;
while((i<=q)&&(j<=r))
{
if(a[i]<a[j])
{
c[k]=a[i];
i=i+1;
k=k+1;
}
else
{
c[k]=a[j];
j=j+1;
k=k+1;
}
}
while(i<=q)
{
c[k] =a[i];
i=i+1;
k=k+1;
}
while(j<=r)
{
c[k]=a[j];
j=j+1;
k=k+1;
}
int l=p;
while(l<=r)
{
a[l]=c[l];
l=l+1;
}
}//Select Case
 break;
QuickSort:

int i,j,n;
int pivot = 100;
int a[n];
int temp;
char answer;

cout<<"A. Ascending or B. Descending"<<endl;
cin>>answer; 
switch (answer)
{
case 'A':
cout<<"How many number do you want to sort?"<<endl;
cin>>n;
for(int x=1; x<=n; x++)
{
cout<<"Enter "<<"number "<<x<<":"<<endl;
cin>>a[x];
} 
for (int i = 1; i<=n; i++)
for (int j = 1; j<=n; j++)
{
if (a[i]>pivot)
{
temp = a[j];
a[j] = a[i];
a[i] = temp;
}
}
for (int i = 1; i<=n; i++)
for (int j = 1; j<=n; j++)
{
if (a[j]>a[i])
{
temp = a[j];
a[j] = a[i];
a[i] = temp;
}
}
cout<<"The ascending sorted numbers are:"<<endl;
for (int x = 1; x<=n; x++)
{
cout<<a[x]<<", ";
}

break;

case 'B': 
cout<<"How many number do you want to sort?"<<endl;
cin>>n;
for(int x=1; x<=n; x++)
{
cout<<"Enter "<<"number "<<x<<":"<<endl;
cin>>a[x];
} 
for (int i = 1; i<=n; i++)
for (int j = 1; j<=n; j++)
{
if (a[j]>pivot)
{
temp = a[j];
a[j] = a[i];
a[i] = temp;
}
}
for (int i = 1; i<=n; i++)
for (int j = 1; j<=n; j++)
{
if (a[i]>a[j])
{
temp = a[j];
a[j] = a[i];
a[i] = temp;
}
}
cout<<"The descending sorted numbers are:"<<endl;
for (int x = 1; x<=n; x++)
{
cout<<a[x]<<", ";
}
break;


default:
cout <<"\nInvalid command,\n Please Run Again!";
break;

}
exit:
{ getch();
return 0;
}//exit statement


This post has been edited by jimblumberg: 23 March 2013 - 06:36 AM
Reason for edit:: Fixed Code Tags.


Is This A Good Question/Topic? 0
  • +

Replies To: Switch (select) error

#2 buffalobill  Icon User is offline

  • D.I.C Head

Reputation: 21
  • View blog
  • Posts: 188
  • Joined: 08-July 08

Re: Switch (select) error

Posted 22 March 2013 - 09:29 PM

Yes you need a closing brace for the switch function
Was This Post Helpful? 1
  • +
  • -

#3 HanJiyoon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 22-March 13

Re: Switch (select) error

Posted 22 March 2013 - 09:40 PM

Hmm. In what line where should I put ? D:
Was This Post Helpful? 0
  • +
  • -

#4 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1352
  • View blog
  • Posts: 4,654
  • Joined: 19-February 09

Re: Switch (select) error

Posted 22 March 2013 - 09:41 PM

The format is

switch (select) then open bracket "{", and then close bracket "}"

switch()
{


}


Was This Post Helpful? 1
  • +
  • -

#5 HanJiyoon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 22-March 13

Re: Switch (select) error

Posted 22 March 2013 - 09:54 PM

I did that but ... in
InsertionSort:
line .. stated invalid function declaration
Was This Post Helpful? 0
  • +
  • -

#6 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1352
  • View blog
  • Posts: 4,654
  • Joined: 19-February 09

Re: Switch (select) error

Posted 22 March 2013 - 10:18 PM

You can put the bubble sort code in the case block.

switch (select)
{
  case 1: // goto BubbleSort;
          // BubbleSort:
  {
    int array[100], n, c, d, swap;

    cout>>"Enter number of elements\n";
    cout>>"Enter integers\n", n;

    for (c = 0; c < n; c++)
      cin>>&array[c]);

    for (c = 0 ; c < ( n - 1 ); c++)
    {
      for (d = 0 ; d < n - c - 1; d++)
      {
        if (array[d] > array[d+1]) /* For decreasing order use < */
        {
          swap = array[d];
          array[d] = array[d+1];
          array[d+1] = swap;
        }
      }
    }

    cout>>"Sorted list in ascending order:\n";

    for ( c = 0 ; c < n ; c++ )
      cout>>"%d\n", array[c];

  } // end BubbleSort: case 1
  break;
  
  case 2: //goto InsertionSort;
  case 3: //goto MergeSort;  
  case 4: //  goto QuickSort;
  default: cout<<"Invalid Choice!";
   //goto exit;
} // end switch


Was This Post Helpful? 1
  • +
  • -

#7 HanJiyoon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 22-March 13

Re: Switch (select) error

Posted 23 March 2013 - 06:30 AM

I did that in that way .. but still that is the error. :(
Was This Post Helpful? 0
  • +
  • -

#8 jimblumberg  Icon User is online

  • member icon


Reputation: 4098
  • View blog
  • Posts: 12,679
  • Joined: 25-December 09

Re: Switch (select) error

Posted 23 March 2013 - 06:40 AM

You need to find an indentation style you like and use it consistently. This will make spotting problems like you are having easier.

Your problem is that everything past line 16 is not in a function. Also stop using goto and learn one of the other looping constructs and in this program use functions instead of the goto.

Jim
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1