2013-03-02 4 views
0

Ok, donc j'essaye d'écrire une main où il demandera à l'utilisateur d'entrer un nombre de 1 à 6 et si le nombre est de 6 il finira le programme. si elle est supérieure à 6, il vous sera demandé de ressaisir le numéro. La chose est, quand je l'exécute, il ne vérifie pas les déclarations "si" et va automatiquement à cette ligne "s'il vous plaît entrer une autre option"Confus au sujet de pourquoi cette principale ne fonctionne pas

des idées pourquoi mon programme ferait quelque chose comme ça? MISE À JOUR: Je dis qu'il ignore automatiquement toutes les instructions if et pose la dernière question de la boucle while.

int main() 
{ 
    int userChoice = 0; 

    print(); //printing all of the options. 

    cout << "Please enter one of the options listed below" <<endl; 
    cin >> userChoice; 

    while(userChoice != 6)// 6 = the user wishing the end the program when they press 6. 
    { 
     if(userChoice == 1) //adding integer to the front of the list 
     { 
      addValueFront(); 
     } 
     else if(userChoice == 2)//adding integer to the back of the list 
     { 
      addValueBack(); 
     } 
     else if(userChoice == 3)//removing from the list 
     { 
      int n = 0; 
      cout << "Please enter the integer you wish to remove" << endl; 
      cin >> n; 
      removeValue(n); 
     } 
     else if(userChoice == 4)//printing the list 
     { 
      printList(); 
     } 
     else if(userChoice == 5)//printing the number of items from the list 
     { 
      printItem(); 
     } 

     else 
     { 
      cout << "The number you have entered is too high. Please try again" << endl; 
      cin >> userChoice; 
     } 
     cout << "please enter another option" <<endl; 

     cin >> userChoice; //sets up which option the user can choose from. 
    } 
} 
+0

Le titre n'est pas trop descriptif. – chris

+4

Définir "ne fonctionne pas". Je parie que cela fonctionne et fait exactement ce que le code lui dit de faire. –

+0

Voulez-vous dire 'while (userChoice! = 6)'? –

Répondre

1

Je pense que le programme suivant est ce que vous voulez:

int main()                  
{                    
    int userChoice = 0;               

    print(); //printing all of the options.          

    cout << "Please enter one of the options listed below" <<endl;                         
    do // 6 = the user wishing the end the program when they press 6.     
    {                    
    cin >> userChoice; 
    if(userChoice > 6)              
    {                  
     cout << "The number you have entered is too high. Please try again" << endl; 
     cout << "please enter another option" <<endl;       
    }                  
    else if(userChoice == 1) //adding integer to the front of the list   
    {                   
     addValueFront();               
    }                   
    else if(userChoice == 2)//adding integer to the back of the list   
    { 
     addValueBack();               
    } 
    else if(userChoice == 3)//removing from the list       
    { 
     int n = 0;                
     cout << "Please enter the integer you wish to remove" << endl;   
     cin >> n;                 
     removeValue(n);               
    }                   
    else if(userChoice == 4)//printing the list         
    {                   
     printList();                
    }                   
    else if(userChoice == 5)//printing the number of items from the list  
    {                   
     printItem();                
    }                   
    } while(userChoice != 6);              
}                    
+0

Wow !! J'ai oublié tout sur les boucles do-while! Ceci est un épargnant de vie! :) Je vous remercie! – booky99

+1

Vous avez également oublié l'instruction switch :-P @ booky99 –

+0

Je suis d'accord avec Aniket. Changer les déclarations sur ce genre de chose rendra votre travail beaucoup plus facile. – user1066524

2

Ajoutez un "continuer" à la fin de votre bloc else.

cout << "The number you have entered is too high. Please try again" << endl; 
cin >> userChoice; 
continue; 
+0

+1 Ceci doit être le changement le plus minime pour résoudre le problème du PO (le code a d'autres problèmes, mais cette ligne répondra à ce que je pense qu'il pose dans cette question). – WhozCraig

0

S'il vous plaît se référer aux réponses pour this question et this question. Vous devez appeler cin.clear() et cin.ignore() avant chaque < < cin, pour vider les tampons du clavier afin qu'il se comporte correctement et régulièrement.

En outre, vous devez supprimer le cin du bloc else car il est logiquement incorrect.

0

Utilisez l'interrupteur ce serait la meilleure option.

int main() 
{ 
    int userChoice = 0; 

    print(); //printing all of the options. 

    cout << "Please enter one of the options listed below" <<endl; 
    cin >> userChoice; 

    while(1)// 6 = the user wishing the end the program when they press 6. 
    { 
     if(userChoice == 1) //adding integer to the front of the list 
     { 
      addValueFront(); 
     } 
     else if(userChoice == 2)//adding integer to the back of the list 
     { 
      addValueBack(); 
     } 
     else if(userChoice == 3)//removing from the list 
     { 
      int n = 0; 
      cout << "Please enter the integer you wish to remove" << endl; 
      cin >> n; 
      removeValue(n); 
     } 
     else if(userChoice == 4)//printing the list 
     { 
      printList(); 
     } 
     else if(userChoice == 5)//printing the number of items from the list 
     { 
      printItem(); 
     } 
     else if(userChoice == 6)// 6 = the user wishing the end the program when they press 6. 
     { 
      return 0; 
     } 
     else 
     { 
      cout << "The number you have entered is too high. Please try again" << endl; 
      cin >> userChoice; 
     } 
    } 
}