Към съдържанието

Програма за подаване на глас

  • tedo21
  • 8 870 прочитания

Урокът е от архива на сайта и е писан преди години. Някои бутони и менюта в програмите вече може да изглеждат различно.

Ето как можете да си направите програма за подаване на глас. Ползваме операторите "if, switch, case, break, default, while и т.н.

 

// Voting.cpp : Defines the entry point for the console application.

//

#include "stdafx.h"

#include <iostream>

using namespace std;

int main()

{

int age;

char name[16];

char city[25];

int selection;

cout<< "Hello, you've come here to vote? How old are you?" <<endl;

cin>> age;

if (age>=18)

{

cout<< "Ok, you can vote for the mayor elections. What's your name?" <<endl;

cin>> name;

cout<< "Ok, " << name << ", where do you live?" <<endl;

cin>> city;

cout<< city << ", is that right? Ok, who do you want to vote for?" <<endl;

cout<< "This is the list of the candidates, pick the number of the person" <<endl;

cout<< "you want to vote for." <<endl;

cout<< "" <<endl;

cout<< "1.David" <<endl;

cout<< "2.George" <<endl;

cout<< "3.Ivan" <<endl;cout<< "4.Hristo" <<endl;

cin>> selection;

switch (selection)

{

case 1: cout<< "You have voted for David. Goodbye and have a nice day!" <<endl;

break;

case 2: cout<< "You have voted for George. Goodbye and have a nice day!" <<endl;

break;

case 3: cout<< "You have voted for Ivan. Goodbye and have a nice day!" <<endl;

break;

case 4: cout<< "You have voted for Hristo. Goodbye and have a nice day!" <<endl;

break;

default: cout<< "The person you have selected is not in the list." <<endl;

while (selection>4)

{

cout<< "Please enter a valid number." <<endl;

cin>> selection;

switch (selection)

{

case 1: cout<< "You have voted for David. Goodbye and have a nice day!" <<endl;

break;

case 2: cout<< "You have voted for George. Goodbye and have a nice day!" <<endl;

break;

case 3: cout<< "You have voted for Ivan. Goodbye and have a nice day!" <<endl;

break;

case 4: cout<< "You have voted for Hristo. Goodbye and have a nice day!" <<endl;break;

}

}

}

}

else

{

cout<< "Sorry, you cannot vote." <<endl;return 0;

}

return 0;

}

Споделете: