Welcome to Dream.In.Code
Become a C++ Expert!

Join 149,548 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,559 people online right now. Registration is fast and FREE... Join Now!




Using Structures

 
Reply to this topicStart new topic

Using Structures, Use a structure to read value and evaluate the results of a survey

k_tec
30 Oct, 2007 - 11:40 AM
Post #1

New D.I.C Head
*

Joined: 7 Jun, 2007
Posts: 11


My Contributions
Question: A survey of 10 pop artists is made. Each person votes for an artist by specifying the number of the artist(a value from 1 to 10). Each voter is allowed one vote for the artist of his/her choice. The vote is recorded as a number from 1 to 10. The number of voters is unknown beforehand but the votes are terminated by a vote of 0. any vote which is not a number from 1 to 10 is a spoilt vote. A file, vote.txt contains the names of the candidates. The first name is considered as candidate 1, the second as candidate 2, etc. etc. The names are folled by the votes. Write a program using structures to read the data and evaluate the results of the survey. Print the results in alphabetical by artist name and in order by votes received(least votes first). Output to a file.


CODE


#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
int voting(num);  //function prototype

main ()
{
clrscr();

char name[30];
int num_vote;

typedef struct vote {
              name;
              num_vote;
            }ballot;

//declarations
int most_ votes=0;
int spoilt=0;
int valid=0;

FILE * in=fopen("votes.txt","r"); //read from file
FILE * out=fopen("results.txt","w"); //write to file

printf("\nPlease enter the vote of your choice");
scanf("%d", &ballot);

while(ballot !=0) { //terminates when 0 is entered
if(ballot < 1 || ballot > 10) { //if votes is in this range
++spoilt; } //count the number of spoilt votes
if(ballot >= 1 && ballot <= 10) {//if votes is between this range
++valid; } //count the number of valid votes

//Calculates the most votes
if(ballot > most_votes) {
most_votes = ballot; }
scanf("%d", &ballot); }

//bubble sort

//count the number of candidates
for(int d=0; d<10; d++)
{


fprintf(out, "Artist Name is: %s",name);
fprintf(out, "Number of Votes received: %d",num_vote);



close(in);
fclose(out); }

User is offlineProfile CardPM
+Quote Post

jjhaag
RE: Using Structures
30 Oct, 2007 - 11:49 AM
Post #2

me editor am smartastic
Group Icon

Joined: 18 Sep, 2007
Posts: 1,789



Thanked: 2 times
Dream Kudos: 775
Expert In: C,C++

My Contributions
Do you have questions regarding this code? Are there compile-time or run-time errors? If so, you should clarify your question and post your errors or whatever input results in runtime errors. It's not really clear what you're asking here.

-jjh
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/7/09 09:38PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month