help - closing brakets to make program output correctly

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • wuzertheloser
    New Member
    • Oct 2006
    • 22

    help - closing brakets to make program output correctly

    The correct program should look like this:

    Total number of voting cards: 55

    Votes for candidate 1: 1
    Votes for candidate 2: 10
    Votes for candidate 3: 8
    Votes for candidate 4: 13
    Votes for candidate 5: 8
    Votes for candidate 6: 12
    Votes for candidate 7: 3

    Percent votes for Mr. Adams : 1.82 %
    Percent votes for Mr. Camacho : 18.18 %
    Percent votes for Mr. Copeland : 14.55 %
    Percent votes for Mr. Simongton : 23.64 %
    Percent votes for Mr. Schwarz : 14.55 %
    Percent votes for Mr. Smith : 21.82 %
    Percent votes for Mr. Steinberg : 5.45 %


    I can make it show up in two different ways:

    Total number of voting cards : 55

    Votes for candidate 1: 1
    Votes for candidate 2: 10
    Votes for candidate 3: 8
    Votes for candidate 4: 13
    Votes for candidate 5: 8
    Votes for candidate 6: 12
    Votes for candidate 7: 3

    Percent votes for Mr. Steinberg : 1.82 percent
    Percent votes for Mr. Steinberg : 18.18 percent
    Percent votes for Mr. Steinberg : 14.55 percent
    Percent votes for Mr. Steinberg : 23.64 percent
    Percent votes for Mr. Steinberg : 14.55 percent
    Percent votes for Mr. Steinberg : 21.82 percent
    Percent votes for Mr. Steinberg : 5.45 percent

    or

    Total number of voting cards : 55

    Votes for candidate 1: 1
    Votes for candidate 2: 10
    Votes for candidate 3: 8
    Votes for candidate 4: 13
    Votes for candidate 5: 8
    Votes for candidate 6: 12
    Votes for candidate 7: 3

    Percent votes for Mr. Adams : 1.82 percent
    Percent votes for Mr. Camacho : 1.82 percent
    Percent votes for Mr. Copeland : 1.82 percent
    Percent votes for Mr. Simongton : 1.82 percent
    Percent votes for Mr. Schwarz : 1.82 percent
    Percent votes for Mr. Smith : 1.82 percent
    Percent votes for Mr. Steinberg : 1.82 percent

    depending on how i close the brackets at the end of my code.
    my code is as follows...

    Code:
    #include <stdio.h>
    main()
    {
    FILE *input1;
    FILE *input2;
    FILE *input3;
    FILE *output1;
    int votes, cand[7], row, ones, q, cands;
    char vote[81];
    input1 = fopen("q_in1.dat", "r");
    output1 = fopen("q_out1.dat", "w");
    if((input1 == NULL) || (output1 == NULL)){
            printf("Error opening the file\n");
            exit(911);}
    row=0;
    ones=0;
    for(q=0; q<7; q++) cand[q]=0;
    while(fgets(vote, 81, input1) !=NULL){
    row++;
    ones=0;
    for(q=0; q<7; q++){
            if(vote[q]=='1'){
                    ones++;
                    cands=q;}}
            if(ones==1)cand[cands]++;
    }
    printf("\nTotal number of voting cards : %d\n\n", row);
    for(q=0; q<7; q++){
    printf("Votes for candidate %d: %4d\n", q+1, cand[q]);
    fprintf(output1, "%d\n", cand[q]);}
            printf("\n");
    fclose(input1);
    fclose(output1);
    
    char cand_name[41], name[52], surname[62];
    int votes_recieved;
    double p;
    input2 = fopen("q_in2.dat", "r");
    input3 = fopen("q_out1.dat", "r");
    if((input2 == NULL) || (input3 == NULL)){
            printf("Error opening the file\n");
            exit(911);
    }
    while(fscanf(input3, "%d", &votes_recieved) != EOF){
            p = (votes_recieved/55.)*100.;
    while(fgets(cand_name, 41, input2) != NULL){
            sscanf(cand_name, "%s %s", name, surname);
            printf("Percent votes for Mr. %-16s: %4.2f percent\n", surname, p);}}
    
    fclose(input2);
    fclose(input3);
    }
    The ways I can get the two different outputs is by varying in the brakets at the end in the

    Code:
    while(fscanf(input3, "%d", &votes_recieved) != EOF){
            p = (votes_recieved/55.)*100.;
    while(fgets(cand_name, 41, input2) != NULL){
            sscanf(cand_name, "%s %s", name, surname);
            printf("Percent votes for Mr. %-16s: %4.2f percent\n", surname, p);}}
    if i put the one braket after
    sscanf(cand_nam e, "%s %s", name, surname);
    then the output becomes what the first output gives, with the names incorrect, but percentages correct

    if i leave it the way it is
    then the output becomes what the second output gave, with the names correct, but percentages incorrect.
  • sivadhas2006
    New Member
    • Nov 2006
    • 142

    #2
    Hi,

    Can you post the contents in the q_in1.dat and q_in2.dat file.

    Regards,
    M.Sivadhas.

    Comment

    • sivadhas2006
      New Member
      • Nov 2006
      • 142

      #3
      Hi,

      Use this code.
      And post the result.
      Code:
      #include <stdio.h>
      #include <process.h>
      
      #define  NUMBER_OF_CANDIDATES    7
      #define  CANDIDATE_NAME_SIZE     41
      
      /*
       * Error Codes
       */
      
      // Failed to open for reading.
      #define  ERR_FTO_VOTE_FILE_FR       100
      #define  ERR_FTO_CAND_NAME_FILE_FR  101
      #define  ERR_FTO_RESULT_FILE_FR     102
      
      // Failed to open for writing.
      #define  ERR_FTO_RESULT_FILE_FW     103
      
      int main()
      {  
         FILE 
            *fpVote = NULL,
            *fpVoteResult = NULL,
            *fpCandName = NULL;      
         int
            nIndex = 0, 
            nVoteCount = 0,       
            nCandIndex = 0;
            nVotingCards = 0;
         int 
            nCandidate[NUMBER_OF_CANDIDATES];
         char 
            szVote[81];
      
         // Open the vote file for reading.
         fpVote = fopen("q_in1.dat", "r");
         if(fpVote == NULL)
         {
            perror("Failed to open the vote file for reading.\nError");
            exit(ERR_FTO_VOTE_FILE_FR);
         }
      
         // Open the vote result file for writing.
         fpVoteResult = fopen("q_out1.dat", "w");
         if(fpVoteResult == NULL)
         {
            perror("Failed to open the vote result file for writing.\nError");
            exit(ERR_FTO_RESULT_FILE_FW);
         }
      
         // Initialize the variables.   
         nVoteCount = 0;   
         nVotingCards = 0;
         for(nIndex = 0; nIndex < NUMBER_OF_CANDIDATES; nIndex++)
            nCandidate[nIndex] = 0;
      
         while(fgets(szVote, 81, fpVote) !=NULL)
         {
            nVotingCards++;
            nVoteCount = 0;
            for(nIndex = 0; nIndex < NUMBER_OF_CANDIDATES; nIndex++)
            {
               if(szVote[nIndex] == '1')
               {
                  nVoteCount++;
                  nCandIndex = nIndex;
               }
            }
      
            // If it is more than one means it is consider as invalid vote.
            if(nVoteCount == 1)
            {
               // Valid vote. 
               // So Increase the vote count for the candidate.
               nCandidate[nCandIndex]++;
            }
         }
      
         printf("\nTotal number of voting cards : %d\n\n", nVotingCards);
         for(nIndex = 0; nIndex < NUMBER_OF_CANDIDATES; nIndex++)
         {
            printf("Votes for candidate %d: %4d\n", nIndex+1, nCandidate[nIndex]);
            fprintf(fpVoteResult, "%d\n", nCandidate[nIndex]);
         }
         printf("\n");
      
         // Close the opened files.
         fclose(fpVote);
         fclose(fpVoteResult);
         
         char 
            szCandidateName[CANDIDATE_NAME_SIZE], 
            szName[52], 
            szSurName[62];
         int 
            nVotesRecieved = 0;
         double 
            dPercentage = 0;
      
         // Open the candidate name file for reading.
         fpCandName = fopen("q_in2.dat", "r");
         if(fpCandName == NULL)
         {
            perror("Failed to open the candidate name file for reading.\nError");
            exit(ERR_FTO_CAND_NAME_FILE_FR);      
         }
      
         // Open the vote result file for reading.
         fpVoteResult = fopen("q_out1.dat", "r");
         if(fpVoteResult == NULL)
         {
            perror("Failed to open the vote result file for reading.\nError");
            exit(ERR_FTO_RESULT_FILE_FR);
         }
      
         while (fscanf(fpVoteResult, "%d", &nVotesRecieved) != EOF)
         {
            // dPercentage = (nVotesRecieved / 55.) * 100.;
            // May be nVotingCards insted of 55.
            // The reason may be the percentage is with respect to the voting cards.
            dPercentage = (nVotesRecieved / (double)nVotingCards) * 100.;
            
            // while(fgets(szCandidateName, CANDIDATE_NAME_SIZE, fpCandName) != NULL)
            if(fgets(szCandidateName, CANDIDATE_NAME_SIZE, fpCandName) == NULL)
            {
               // Fail to get the candidate name to print the percentage.
               break;
            }
            else
            {
               // Get the sur name from the candidate name and print the result.
               sscanf(szCandidateName, "%s %s", szName, szSurName);
               printf("Percent votes for Mr. %-16s: %4.2f percent\n", szSurName, dPercentage);
            }
         }
         
         // Close the opened files.
         fclose(fpCandName);
         fclose(fpVoteResult);
         return 0;   
      }
      Regards,
      M.Sivadhas.

      Comment

      • pavanikondru
        New Member
        • Oct 2006
        • 2

        #4
        #include<iostre am.h>
        #include<conio. h>
        #include<math.h >
        void main()
        {

        float a,b,c,r1,d,r2,e ;

        cout<<"\n enter the values of three no.s";
        cin>>a>>b>>c;
        d=(b*b)-(4*a*c);
        e=sqrt(d);
        r1=(-b+e)/(2*a);
        r2=(-b-e)/(2*a);
        cout<<"\n root1="<<r1;
        cout<<"\n root2="<<r2;
        if (d>0)
        cout<<"\nroots are different "<<r2<<" "<<r1;
        else if (d==0)
        cout<<"\n roots are equal"<<r1;

        else
        cout<<"\n roots are imaginary";
        getch();
        }

        Comment

        Working...