Switch C++

ThePirate

What is folding?
Boas malta, tenho aqui uma pequena duvida...
Tenho o seguinte código:
Código:
#include <cstdlib>
#include <iostream>
main()
{
    
    int test;
    printf("Escolha a letra da drive que quer formatar.\n");
    printf("Letra da drive: ");
    scanf("%d",&test);
    switch ( test )
    {
  case 1 :
system("format c:");
    break;
  case 2 :
system("format d:");
    break;
  case 3 :
system("end");
    break;
  default : 
    // Process for all other cases.
    system("Pause");
}
}

O meu problema é que assim tenho de inserir o número da case, ou seja "1" ou "2"...
Queria saber como faço para inserir apenas a letra.. Ou seja...
Ser a letra inserida for "e" automaticamente vai para a "case 4", que é para formatar a drive E.

Como faço?
 
Em vez de estares a receber um número inteiro recebes uma string e fazes a mesma coisa.

Código:
#include <iostream>
#include <string>

using namespace std;

int main()
{
    
    string drive;
    cout << Escolha a letra da drive que quer formatar.\n";
    cout << "Letra da drive: ";
    getline(cin, drive);
    switch ( drive )
    {
  case "c" :
system("format c:");
    break;
  case "d" :
system("format d:");
    break;
  case "end" :
system("end");
    break;
  default : 
    // Process for all other cases.
    system("Pause");
}

return 0;
}
Não te esqueças que a função main deve retornar alguma coisa. O mais correcto é retornar um inteiro.

Anteção, não testei, apenas alterei o teu código, mas deve funcionar.
 
Agradecido, comecei hoje no 10º Informatica e gestão.. tou a fazer uns testes para a disciplina :D

EDIT:
Está a dar o erro "in function int main()"
 
Última edição:
#include <cstdlib>
#include <iostream>
#include <string>

main()
{

int test;
printf("Please select the letter of your Drive.\n");
printf("Drive letter: ");
scanf("%d",&test);
switch (test) {
case 1:
cout << "g is 1";
system("format g:");
break;
case 2:
cout << "h is 2";
system("format h:");
break;
default:
cout << "end";
}
return = 0
}

Continua a dar o mesmo erro?
 
Peço desculpa, switch com strings é em C#, esqueci-me desse pormenor :P

Assim funciona:

Código:
#include <iostream>
#include <string>

using namespace std;

int main()
{

    int drive;
    string drive_aux;
    cout << "Escolha a letra da drive que quer formatar.\n";
        cout << "Letra da drive: ";
    getline(cin, drive_aux);

    if (drive_aux == "c")
        drive = 1;

    else if (drive_aux == "d")
        drive = 2;

    else
    {
        //fazer alguma coisa
    }
    switch ( drive )
    {
    case 1 :
        system("format c:");
        break;
    case 2 :
        system("format d:");
        break;
    case 3 :
        system("end");
        break;
    default : 
        // Process for all other cases.
        system("Pause");
    }

    return 0;
}

Não é muito bonito, mas funciona. Infelizmente não tenho grande tempo para pesquisar mais. O que faz é receber uma string do teclado, e conforme a string dada atribui um valor à variavel drive, e depois faz o switch.

PS: cuidado que nos teus códigos estás a misturar C com C++.
 
Back
Topo