473,386 Members | 1,753 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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...

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. main()
  3. {
  4. FILE *input1;
  5. FILE *input2;
  6. FILE *input3;
  7. FILE *output1;
  8. int votes, cand[7], row, ones, q, cands;
  9. char vote[81];
  10. input1 = fopen("q_in1.dat", "r");
  11. output1 = fopen("q_out1.dat", "w");
  12. if((input1 == NULL) || (output1 == NULL)){
  13.         printf("Error opening the file\n");
  14.         exit(911);}
  15. row=0;
  16. ones=0;
  17. for(q=0; q<7; q++) cand[q]=0;
  18. while(fgets(vote, 81, input1) !=NULL){
  19. row++;
  20. ones=0;
  21. for(q=0; q<7; q++){
  22.         if(vote[q]=='1'){
  23.                 ones++;
  24.                 cands=q;}}
  25.         if(ones==1)cand[cands]++;
  26. }
  27. printf("\nTotal number of voting cards : %d\n\n", row);
  28. for(q=0; q<7; q++){
  29. printf("Votes for candidate %d: %4d\n", q+1, cand[q]);
  30. fprintf(output1, "%d\n", cand[q]);}
  31.         printf("\n");
  32. fclose(input1);
  33. fclose(output1);
  34.  
  35. char cand_name[41], name[52], surname[62];
  36. int votes_recieved;
  37. double p;
  38. input2 = fopen("q_in2.dat", "r");
  39. input3 = fopen("q_out1.dat", "r");
  40. if((input2 == NULL) || (input3 == NULL)){
  41.         printf("Error opening the file\n");
  42.         exit(911);
  43. }
  44. while(fscanf(input3, "%d", &votes_recieved) != EOF){
  45.         p = (votes_recieved/55.)*100.;
  46. while(fgets(cand_name, 41, input2) != NULL){
  47.         sscanf(cand_name, "%s %s", name, surname);
  48.         printf("Percent votes for Mr. %-16s: %4.2f percent\n", surname, p);}}
  49.  
  50. fclose(input2);
  51. fclose(input3);
  52. }
  53.  
The ways I can get the two different outputs is by varying in the brakets at the end in the

Expand|Select|Wrap|Line Numbers
  1. while(fscanf(input3, "%d", &votes_recieved) != EOF){
  2.         p = (votes_recieved/55.)*100.;
  3. while(fgets(cand_name, 41, input2) != NULL){
  4.         sscanf(cand_name, "%s %s", name, surname);
  5.         printf("Percent votes for Mr. %-16s: %4.2f percent\n", surname, p);}}
  6.  
if i put the one braket after
sscanf(cand_name, "%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.
Nov 9 '06 #1
3 1596
sivadhas2006
142 100+
Hi,

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

Regards,
M.Sivadhas.
Nov 9 '06 #2
sivadhas2006
142 100+
Hi,

Use this code.
And post the result.
Expand|Select|Wrap|Line Numbers
  1.  
  2. #include <stdio.h>
  3. #include <process.h>
  4.  
  5. #define  NUMBER_OF_CANDIDATES    7
  6. #define  CANDIDATE_NAME_SIZE     41
  7.  
  8. /*
  9.  * Error Codes
  10.  */
  11.  
  12. // Failed to open for reading.
  13. #define  ERR_FTO_VOTE_FILE_FR       100
  14. #define  ERR_FTO_CAND_NAME_FILE_FR  101
  15. #define  ERR_FTO_RESULT_FILE_FR     102
  16.  
  17. // Failed to open for writing.
  18. #define  ERR_FTO_RESULT_FILE_FW     103
  19.  
  20. int main()
  21. {  
  22.    FILE 
  23.       *fpVote = NULL,
  24.       *fpVoteResult = NULL,
  25.       *fpCandName = NULL;      
  26.    int
  27.       nIndex = 0, 
  28.       nVoteCount = 0,       
  29.       nCandIndex = 0;
  30.       nVotingCards = 0;
  31.    int 
  32.       nCandidate[NUMBER_OF_CANDIDATES];
  33.    char 
  34.       szVote[81];
  35.  
  36.    // Open the vote file for reading.
  37.    fpVote = fopen("q_in1.dat", "r");
  38.    if(fpVote == NULL)
  39.    {
  40.       perror("Failed to open the vote file for reading.\nError");
  41.       exit(ERR_FTO_VOTE_FILE_FR);
  42.    }
  43.  
  44.    // Open the vote result file for writing.
  45.    fpVoteResult = fopen("q_out1.dat", "w");
  46.    if(fpVoteResult == NULL)
  47.    {
  48.       perror("Failed to open the vote result file for writing.\nError");
  49.       exit(ERR_FTO_RESULT_FILE_FW);
  50.    }
  51.  
  52.    // Initialize the variables.   
  53.    nVoteCount = 0;   
  54.    nVotingCards = 0;
  55.    for(nIndex = 0; nIndex < NUMBER_OF_CANDIDATES; nIndex++)
  56.       nCandidate[nIndex] = 0;
  57.  
  58.    while(fgets(szVote, 81, fpVote) !=NULL)
  59.    {
  60.       nVotingCards++;
  61.       nVoteCount = 0;
  62.       for(nIndex = 0; nIndex < NUMBER_OF_CANDIDATES; nIndex++)
  63.       {
  64.          if(szVote[nIndex] == '1')
  65.          {
  66.             nVoteCount++;
  67.             nCandIndex = nIndex;
  68.          }
  69.       }
  70.  
  71.       // If it is more than one means it is consider as invalid vote.
  72.       if(nVoteCount == 1)
  73.       {
  74.          // Valid vote. 
  75.          // So Increase the vote count for the candidate.
  76.          nCandidate[nCandIndex]++;
  77.       }
  78.    }
  79.  
  80.    printf("\nTotal number of voting cards : %d\n\n", nVotingCards);
  81.    for(nIndex = 0; nIndex < NUMBER_OF_CANDIDATES; nIndex++)
  82.    {
  83.       printf("Votes for candidate %d: %4d\n", nIndex+1, nCandidate[nIndex]);
  84.       fprintf(fpVoteResult, "%d\n", nCandidate[nIndex]);
  85.    }
  86.    printf("\n");
  87.  
  88.    // Close the opened files.
  89.    fclose(fpVote);
  90.    fclose(fpVoteResult);
  91.  
  92.    char 
  93.       szCandidateName[CANDIDATE_NAME_SIZE], 
  94.       szName[52], 
  95.       szSurName[62];
  96.    int 
  97.       nVotesRecieved = 0;
  98.    double 
  99.       dPercentage = 0;
  100.  
  101.    // Open the candidate name file for reading.
  102.    fpCandName = fopen("q_in2.dat", "r");
  103.    if(fpCandName == NULL)
  104.    {
  105.       perror("Failed to open the candidate name file for reading.\nError");
  106.       exit(ERR_FTO_CAND_NAME_FILE_FR);      
  107.    }
  108.  
  109.    // Open the vote result file for reading.
  110.    fpVoteResult = fopen("q_out1.dat", "r");
  111.    if(fpVoteResult == NULL)
  112.    {
  113.       perror("Failed to open the vote result file for reading.\nError");
  114.       exit(ERR_FTO_RESULT_FILE_FR);
  115.    }
  116.  
  117.    while (fscanf(fpVoteResult, "%d", &nVotesRecieved) != EOF)
  118.    {
  119.       // dPercentage = (nVotesRecieved / 55.) * 100.;
  120.       // May be nVotingCards insted of 55.
  121.       // The reason may be the percentage is with respect to the voting cards.
  122.       dPercentage = (nVotesRecieved / (double)nVotingCards) * 100.;
  123.  
  124.       // while(fgets(szCandidateName, CANDIDATE_NAME_SIZE, fpCandName) != NULL)
  125.       if(fgets(szCandidateName, CANDIDATE_NAME_SIZE, fpCandName) == NULL)
  126.       {
  127.          // Fail to get the candidate name to print the percentage.
  128.          break;
  129.       }
  130.       else
  131.       {
  132.          // Get the sur name from the candidate name and print the result.
  133.          sscanf(szCandidateName, "%s %s", szName, szSurName);
  134.          printf("Percent votes for Mr. %-16s: %4.2f percent\n", szSurName, dPercentage);
  135.       }
  136.    }
  137.  
  138.    // Close the opened files.
  139.    fclose(fpCandName);
  140.    fclose(fpVoteResult);
  141.    return 0;   
  142. }
  143.  
  144.  
Regards,
M.Sivadhas.
Nov 9 '06 #3
#include<iostream.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();
}
Nov 9 '06 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Tim Clark | last post by:
We've been having a lot of event ID 37 with src W3SVC errors popping up. We have 50+ sites in medium isolation, and i ran IISstate on the two instances of DLLHOST and the one of inetinfo. Is there...
8
by: MattP | last post by:
Ok, with the help of some examples found on the web and some minor modifications on our own, we have a simple and working encrypt and decrypt solution. It runs as a service, watches for files with...
3
by: John | last post by:
I am new to c++ programming and i just want to make a program that takes a users name, then converts it to a hex string, then puts it together in a fixed order. An example output would be: ...
2
by: atreide_son | last post by:
hello all... yes i'm a newbie and i need help. i have an assignment due for class, but I don't know i'm stuck and can't get out from under myself. here's the focus of the program: Write...
6
by: James Brown [MVP] | last post by:
Hi, I am having trouble understanding how the 'const' modifier affects the 'left-right' rule when deciphering c-declarations: const int *x; // x is a pointer to a 'const int' int const *x; ...
2
by: Alex Maghen | last post by:
This is a bit of an abuse of this group. Just a nit, but I'm hoping someone really good with Regular Expressions can help me out here. I need to write a regular expression that will do the...
12
by: adamurbas | last post by:
ya so im pretty much a newb to this whole python thing... its pretty cool but i just started today and im already having trouble. i started to use a tutorial that i found somewhere and i followed...
0
by: yabighelen | last post by:
HELP!!!!! I have to write a program that does this: Create a program that encrypts and decrypts a message... Inputs: A typed message Output: When the encrypt button is clicked the input...
6
by: cdm2883 | last post by:
here is my code, i am having trouble with getting my brakets in the right place so i can run my program. package mooredMod2; import java.util.Scanner; import java.util.Random; import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.