Home > OS >  Changing If Else statement to Loop in C
Changing If Else statement to Loop in C

Time:11-26

I'm very new to coding. Is there a way to change this If else statement to a loop to sort the list of this books to ascending and descending order or the number of (books per subject).

Basically it emulates a library system where you choose a subject then you will see the unsorted books with the quantity before the names of the books, and last you will choose how do you want to see the list either ascending or descending order list.

#include<iostream>
using namespace std;

int main()
{
    int subject;
    cout<<"What subject? \n" ;
    cout<<"[1] English \n" ;
    cout<<"[2] Mathematics \n" ;
    cout<<"[3] Science \n" ;
    cout<<"[4] Filipino \n\n" ;
    cin>>subject;
    switch(subject) {
        case 1 : cout<<"How do you want to see the books on this subject? \n\n";
                 cout<<"3 Spelling\n" ;
                 cout<<"5 Alphabet\n" ;
                 cout<<"6 Punctuations\n" ;
                 cout<<"2 Grammar\n" ;
                 cout<<"5 Alphabet\n" ;
                 cout<<"1 Literature\n\n" ;
        {

        int a = 1;
        int ans;

        ans=a;

        int inp;
        cout<<"type 1 for ascending and 2 for descending\n \n"<<endl;
        cin>>inp;

        if (inp==ans) {
        cout<<"Ascending\n\n" ;
        cout<<"1 Literature\n" ;
        cout<<"2 Grammar\n" ;
        cout<<"3 Spelling\n" ;
        cout<<"4 Short Stories\n" ;
        cout<<"5 Alphabet\n" ;
        cout<<"6 Punctuations\n" ;}
        else{
        cout<<"Descending\n\n" ;
        cout<<"6 Punctuations \n" ;
        cout<<"5 Alphabet \n" ;
        cout<<"4 Short Stories \n" ;
        cout<<"3 Spelling \n" ;
        cout<<"2 Grammar\n" ;
        cout<<"1 Literature\n" ;
       
         } 
         } 
        break;
        case 2 : cout<<"How do you want to see the books on this subject? \n";
                 cout<<"3 Calculus\n" ;
                 cout<<"1 Algebra\n" ;
                 cout<<"6 Trigonometry\n" ;
                 cout<<"4 Graphs\n" ;
                 cout<<"2 Functiosn\n" ;
                 cout<<"5 Geometry\n\n" ;
        {

        int a = 1;
        int ans;

        ans=a;

        int inp;
        cout<<"type 1 for ascending and 2 for descending\n \n"<<endl;
        cin>>inp;

        if (inp==ans) {
        cout<<"Ascending\n\n" ;
        cout<<"1 Algebra\n" ;
        cout<<"2 Functiosn\n" ;
        cout<<"3 Calculus\n" ;
        cout<<"4 Graphs\n" ;
        cout<<"5 Geometry\n" ;
        cout<<"6 Trigonometry\n" ;}
        else{
        cout<<"Descending\n\n" ;
        cout<<"6 Trigonometry \n" ;
        cout<<"5 Geometry \n" ;
        cout<<"4 Graphs \n" ;
        cout<<"3 Calculus \n" ;
        cout<<"2 Functions \n" ;
        cout<<"1 Algebra \n" ;
       
         } 
         }
        break;
        case 3 : cout<<"How do you want to see the books on this subject? \n";
                 cout<<"3 Solar system\n" ;
                 cout<<"1 Living things\n" ;
                 cout<<"6 Newton's Theory\n" ;
                 cout<<"4 Energy\n" ;
                 cout<<"2 Non-living things\n" ;
                 cout<<"5 Elements\n\n" ;
        {

        int a = 1;
        int ans;

        ans=a;

        int inp;
        cout<<"type 1 for ascending and 2 for descending\n \n"<<endl;
        cin>>inp;

        if (inp==ans) {
        cout<<"Ascending\n\n" ;
        cout<<"1 Living things\n" ;
        cout<<"2 Non-living things\n" ;
        cout<<"3 Solar system\n" ;
        cout<<"4 Energy\n" ;
        cout<<"5 Elements\n" ;
        cout<<"6 Newton's Theory\n" ;}
        else{
        cout<<"Descending\n\n" ;
        cout<<"6 Newton's Theory\n" ;
        cout<<"5 Elements \n" ;
        cout<<"4 Energy \n" ;
        cout<<"3 Solar system \n" ;
        cout<<"2 Non-living things\n" ;
        cout<<"1 Living things\n" ;
       
         } 
         }
        break;
        case 4 : cout<<"How do you want to see the books on this subject? \n";
                 cout<<"3 Gramatika\n" ;
                 cout<<"1 Pandiwa\n" ;
                 cout<<"6 Tamang Pagbigkas\n" ;
                 cout<<"4 Bantas\n" ;
                 cout<<"5 Mga kilalang manunulat\n" ;
                 cout<<"2 Maikling Kwento\n\n" ;
        {

        int a = 1;
        int ans;

        ans=a;

        int inp;
        cout<<"type 1 for ascending and 2 for descending\n \n"<<endl;
        cin>>inp;

        if (inp==ans) {
        cout<<"Ascending\n\n" ;
        cout<<"1 Pandiwa\n" ;
        cout<<"2 Maikling Kwento\n" ;
        cout<<"3 Gramatika\n" ;
        cout<<"4 Bantas\n" ;
        cout<<"5 Mga kilalang manunulat\n" ;
        cout<<"6 Tamang Pagbigkas\n" ;}
        else{
        cout<<"Descending\n\n" ;
        cout<<"6 Tamang Pagbigkas \n" ;
        cout<<"5 Mga kilalang manunulat \n" ;
        cout<<"4 Bantas \n" ;
        cout<<"3 Gramatika \n" ;
        cout<<"2 Maikling kwento\n" ;
        cout<<"1 Pandiwa\n" ;
       
         } 
         }
        break; 
        default : cout<<"Choose between 1,2,3,and 4";
        }
    
}

CodePudding user response:

You need to improve a lot in your coding as well as in designing this application. Firstly you want to convert your if else statement to loop, to do you can use the while loop and add one more option to user to exit from menu. All the menu (where you are asking user to enter choice) should have exit option. for example below

case 1 : cout<<"How do you want to see the books on this subject? \n\n";
                 cout<<"3 Spelling\n" ;
                 cout<<"5 Alphabet\n" ;
                 cout<<"6 Punctuations\n" ;
                 cout<<"2 Grammar\n" ;
                 cout<<"5 Alphabet\n" ;
                 cout<<"1 Literature\n\n" ;
                 cout<<"0 Exit Menu/Application\n\n";

To rearrange your books by name you can compare the book's name first character ASCII value and rearrange the books in sorted order inside the loop as per Menu selection.

Hope this help you. I am not suggesting here to use a better data structure because keeping in mind you are new in coding.

  •  Tags:  
  • c
  • Related