Jump to content
Fórum Script Brasil
  • 0

[Duvida] Sobre uma estrutura while aninhada com switch


Hzsiul
 Share

Question

Sou iniciante em c++, estou lendo o livro de deitel how to program 3º edição, e me deparei com um programa exemplo que

apresenta comportamento anormal ele não funciona! baixei o mesmo programa do site em english,e o mesmo não funcionou tambêm!

Eu consegui fazer o código funcionar retirando o valor sentinela, mas gostaria de entender o motivo do original não ser funcional.

O objetivo do programa é apenas armazenar as notas e mostrar na tela, usando as estruturas aninhas while e switch com valor sentinela, porem não

consegui fazer o valor sentinela funcionar, quando o valor sentinela é acionado, aparece a mensagem, que deveria aparecer quando você digita-se um caractere inválido, e eu tentei compilar o codigo original tanto em ambiente UNIX e ambiente WINDOWS;

Original:

#include <iostream>
    using std::cout;
    using std::cin;
    using std::endl;
     
    int main()
    {
       int grade,       // one grade
           aCount = 0,  // number of A's
           bCount = 0,  // number of B's
           cCount = 0,  // number of C's
           dCount = 0,  // number of D's
           fCount = 0;  // number of F's
     
       cout << "Enter the letter grades." << endl
            << "Enter the EOF character to end input." << endl;
     
       while ( ( grade = cin.get() ) != EOF ) {
     
          switch ( grade ) {      // switch nested in while
     
             case 'A':  // grade was uppercase A
             case 'a':  // or lowercase a
                ++aCount;        
                break;  // necessary to exit switch
     
             case 'B':  // grade was uppercase B
             case 'b':  // or lowercase b
                ++bCount;        
                break;
     
             case 'C':  // grade was uppercase C
             case 'c':  // or lowercase c
                ++cCount;        
                break;
     
             case 'D':  // grade was uppercase D
             case 'd':  // or lowercase d
                ++dCount;        
                break;
     
             case 'F':  // grade was uppercase F
             case 'f':  // or lowercase f
                ++fCount;        
                break;
     
             case '\n': // ignore newlines,  
             case '\t': // tabs,
             case ' ':  // and spaces in input
                break;
     
             default:   // catch all other characters
                cout << "Incorrect letter grade entered."
                     << " Enter a new grade." << endl;
                break;  // optional
          }
       }
     
       cout << "\n\nTotals for each letter grade are:"
            << "\nA: " << aCount
            << "\nB: " << bCount
            << "\nC: " << cCount
            << "\nD: " << dCount
            << "\nF: " << fCount << endl;
     
       return 0;
    }
Do jeito que eu arrumei:
#include <iostream>
    using namespace std;
     
    int main()
    {
       int nentrada,
           aCount = 0,
           bCount = 0,
           cCount = 0,
           dCount = 0,
           fCount = 0;
     
       cout << "Digite as letras correspondentes as notas, e digite z para sair do programa." << endl;
       while (nentrada = cin.get()){
       switch ( nentrada ) {
     
             case 'A':
                     case 'a':
                ++aCount;        
                break;
     
             case 'B':
             case 'b':
                ++bCount;        
                break;
     
             case 'C':
             case 'c':
                ++cCount;        
                break;
     
             case 'D':
             case 'd':
                ++dCount;        
                break;
     
             case 'F':
             case 'f':
                ++fCount;        
                break;
     
             case '\n':
             case '\t':
             case ' ':  
                break;
     
                     case 'z':
                 case 'Z':
             exit(0);
                            break;
     
             default:  
                     break;  }
            system("cls");
                    cout << "Totalizando as notas:"
            << "\nA: " << aCount
            << "\nB: " << bCount
            << "\nC: " << cCount
            << "\nD: " << dCount
            << "\nF: " << fCount << endl;
     
                    cout<<"Digite as letras correspondentes as notas, e digite z para sair do programa."<<endl;
    }
    return 0;
    }

Link to comment
Share on other sites

4 answers to this question

Recommended Posts

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share



  • Forum Statistics

    • Total Topics
      150.9k
    • Total Posts
      648.8k
×
×
  • Create New...