944,033 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1845
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Nov 1st, 2009
0

just a quick question here

Expand Post »
does anyone know how to convert c program code into c++ program code? I have program written in c code, but need the same program and its functions in c++. I can post code if needed. Thanks.
Reputation Points: 10
Solved Threads: 0
Light Poster
dinamit875 is offline Offline
48 posts
since Mar 2009
Nov 1st, 2009
0
Re: just a quick question here
Well, C++ can compile almost any C code, except for a few minors differences. Post the code and I'll see.
Reputation Points: 1429
Solved Threads: 129
Posting Virtuoso
William Hemsworth is offline Offline
1,542 posts
since Mar 2008
Nov 1st, 2009
0
Re: just a quick question here
I know that c++ can compile most of c code, but i just need c++ code not c code heres the code:
C++ Syntax (Toggle Plain Text)
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. void addTeam(char[50], int);
  5. void calculateResult(int, int, int, int);
  6.  
  7. char newTeam[50];
  8. int menuChoice = 0;
  9. int numOfTeams = 0;
  10. int team1, team2, team1Score, team2Score;
  11.  
  12. typedef struct
  13. {
  14. char name[50];
  15. int points, goalsFor, goalsAgainst, played, won, lost, drawn;
  16. } team;
  17.  
  18. team teams[6];
  19.  
  20. void sortTable(team[]);
  21.  
  22. int main(void)
  23. {
  24. int i;
  25. while (menuChoice != 4)
  26. {
  27. printf("Football League\n\n");
  28. printf("1. Add team\n");
  29. printf("2. Display league table\n");
  30. printf("3. Add result\n");
  31. printf("4. Quit\n");
  32. scanf("%d", &menuChoice);
  33.  
  34. if (menuChoice == 1)
  35. {
  36. if (numOfTeams == 6)
  37. printf("Error: Maximum amount of teams has been entered");
  38. else
  39. {
  40. printf("Add new team\n");
  41. fgets(newTeam, sizeof(newTeam), stdin);
  42. newTeam[strlen(newTeam)-1] = '\0';
  43. fgets(newTeam, sizeof(newTeam), stdin);
  44. newTeam[strlen(newTeam)-1] = '\0';
  45. addTeam(newTeam, numOfTeams);
  46. numOfTeams++;
  47. printf("\nNew team added\n");
  48. }
  49. }
  50. else if (menuChoice == 2)
  51. {
  52. printf("\t\tP\tW\tD\tL\tF\tA\tT\n\n\n");
  53. for ( i = 0; i < 6; i++)
  54. {
  55. printf("%s\t\t%d\t%d\t%d\t%d\t%d\t%d\t%d\n",
  56. teams[i].name, teams[i].played, teams[i].won,teams[i].drawn,
  57. teams[i].lost, teams[i].goalsFor, teams[i].goalsAgainst,
  58. teams[i].points);
  59. }
  60. }
  61. else if (menuChoice == 3)
  62. {
  63. printf("Enter first team playing:\n");
  64. scanf("%d", &team1);
  65. printf("Enter second team playing:\n");
  66. scanf("%d", &team2);
  67. printf("Enter first teams score:\n");
  68. scanf("%d", &team1Score);
  69. printf("Enter second teams score:\n");
  70. scanf("%d", &team2Score);
  71. calculateResult(team1, team2, team1Score, team2Score);
  72. }
  73.  
  74. }
  75. return 0;
  76. }
  77.  
  78. void addTeam(char *teamName, int i)
  79. {
  80. strcpy(teams[i].name, teamName);
  81. teams[i].points = 0;
  82. teams[i].goalsFor = 0;
  83. teams[i].goalsAgainst = 0;
  84. teams[i].played = 0;
  85. teams[i].won = 0;
  86. teams[i].lost = 0;
  87. teams[i].drawn = 0;
  88. }
  89.  
  90. void calculateResult(int t1, int t2, int t1R, int t2R)
  91. {
  92. if (t1R > t2R)
  93. {
  94. teams[(t1-1)].points+=3;
  95. teams[(t1-1)].won++;
  96. teams[(t2-1)].lost++;
  97. }
  98. else if (t2R > t1R)
  99. {
  100. teams[(t2-1)].points+=3;
  101. teams[(t2-1)].won++;
  102. teams[(t1-1)].lost++;
  103. }
  104. else
  105. {
  106. teams[(t2-1)].points++;
  107. teams[(t1-1)].points++;
  108. teams[(t1-1)].drawn++;
  109. teams[(t2-1)].drawn++;
  110. }
  111. teams[(t1-1)].goalsFor+=t1R;
  112. teams[(t2-1)].goalsFor+=t2R;
  113. teams[(t1-1)].goalsAgainst+=t2R;
  114. teams[(t2-1)].goalsAgainst+=t1R;
  115. teams[(t1-1)].played++;
  116. teams[(t2-1)].played++;
  117.  
  118. sortTable(teams);
  119. }
  120.  
  121. void sortTable(team teams1[])
  122. {
  123. team temp[1];
  124. int i, j;
  125. for ( i = 0; i < 6; i++)
  126. {
  127. for ( j = 6; j >=0; j--)
  128. {
  129. if (teams1[j].points > teams1[(j-1)].points)
  130. {
  131. temp[1] = teams1[(j-1)];
  132. teams1[(j-1)] = teams1[j];
  133. teams1[j] = temp[1];
  134. }
  135. }
  136. }
  137. }
I am trying to rewrite it into c++, but nothing good yet, i need to have same functions ect in c++.
Reputation Points: 10
Solved Threads: 0
Light Poster
dinamit875 is offline Offline
48 posts
since Mar 2009
Nov 1st, 2009
0
Re: just a quick question here
Just look up printing and getting input using cout<< and cin>>. Also, use
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. using namespace std;
That seems to be the only things you really need to change on first glance.
I'm sure William or someone else will correct me if I'm wrong.
Last edited by Grn Xtrm; Nov 1st, 2009 at 8:42 pm.
Reputation Points: 100
Solved Threads: 48
Posting Pro in Training
Grn Xtrm is offline Offline
495 posts
since Nov 2008
Nov 1st, 2009
0
Re: just a quick question here
i have made those change to c code but getting another errors and here's the result:
C++ Syntax (Toggle Plain Text)
  1. //#include <string.h>
  2. #include <iostream>
  3. #include <iomanip>
  4. #include <string>
  5. #include <stdio.h>
  6.  
  7. using namespace std;
  8.  
  9. struct TeamInfo
  10. {
  11. char name[50];
  12. int points, goalsFor, goalsAgainst, played, won, lost, drawn;
  13. }
  14. typedef struct team;
  15.  
  16. void addTeam(char[50], int);
  17. void calculateResult(int, int, int, int);
  18. void sortTable(team[]);
  19.  
  20. //sortTable(team, teams1[])
  21.  
  22. int main(void)
  23. {
  24.  
  25. char newTeam[50];
  26. int menuChoice = 0;
  27. int numOfTeams = 0;
  28. int team1, team2, team1Score, team2Score;
  29.  
  30. team teams[6];
  31.  
  32.  
  33. int i;
  34. while (menuChoice != 4)
  35. {
  36. cout << "Football League\n\n" << endl;
  37. cout << "1. Add team\n" << endl;
  38. cout << "2. Display league table\n" << endl;
  39. cout << "3. Add result\n" << endl;
  40. cout << "4. Quit\n" << endl;
  41. cin >> menuChoice;
  42.  
  43. if (menuChoice == 1)
  44. {
  45. if (numOfTeams == 6)
  46. cout << "Error: Maximum amount of teams has been entered" << endl;
  47. else
  48. {
  49. cout << "Add new team\n" << endl;
  50. cin.get(newTeam, sizeof(newTeam), stdin);
  51. newTeam[strlen(newTeam)-1] = '\0';
  52. cin.get(newTeam, sizeof(newTeam), stdin);
  53. newTeam[strlen(newTeam)-1] = '\0';
  54. addTeam(newTeam, numOfTeams);
  55. numOfTeams++;
  56. cout << "\nNew team added\n" << endl;
  57. }
  58. }
  59. else if (menuChoice == 2)
  60. {
  61. cout << "\t\tP\tW\tD\tL\tF\tA\tT\n\n\n" << endl;
  62. for ( i = 0; i < 6; i++)
  63. {
  64. cout << "%s\t\t%d\t%d\t%d\t%d\t%d\t%d\t%d\n",
  65. teams[i].name, teams[i].played, teams[i].won,teams[i].drawn,
  66. teams[i].lost, teams[i].goalsFor, teams[i].goalsAgainst, teams[i].points << endl;
  67. }
  68. }
  69. else if (menuChoice == 3)
  70. {
  71. cout << "Enter first team playing:\n" << endl;
  72. cin >> team1;
  73. cout << "Enter second team playing:\n" << endl;
  74. cin >> team2;
  75. cout << "Enter first teams score:\n" << endl;
  76. cin >> team1Score;
  77. cout << "Enter second teams score:\n" << endl;
  78. cin >> team2Score;
  79. calculateResult(team1, team2, team1Score, team2Score);
  80. }
  81. }
  82. void addTeam(char *teamName, int i)
  83. {
  84. strcpy(teams[i].name, teamName);
  85. teams[i].points = 0;
  86. teams[i].goalsFor = 0;
  87. teams[i].goalsAgainst = 0;
  88. teams[i].played = 0;
  89. teams[i].won = 0;
  90. teams[i].lost = 0;
  91. teams[i].drawn = 0;
  92. }
  93.  
  94. void calculateResult(int t1, int t2, int t1R, int t2R)
  95. {
  96. if (t1R > t2R)
  97. {
  98. teams[(t1-1)].points+=3;
  99. teams[(t1-1)].won++;
  100. teams[(t2-1)].lost++;
  101. }
  102. else if (t2R > t1R)
  103. {
  104. teams[(t2-1)].points+=3;
  105. teams[(t2-1)].won++;
  106. teams[(t1-1)].lost++;
  107. }
  108. else
  109. {
  110. teams[(t2-1)].points++;
  111. teams[(t1-1)].points++;
  112. teams[(t1-1)].drawn++;
  113. teams[(t2-1)].drawn++;
  114. }
  115. teams[(t1-1)].goalsFor+=t1R;
  116. teams[(t2-1)].goalsFor+=t2R;
  117. teams[(t1-1)].goalsAgainst+=t2R;
  118. teams[(t2-1)].goalsAgainst+=t1R;
  119. teams[(t1-1)].played++;
  120. teams[(t2-1)].played++;
  121.  
  122. sortTable(teams);
  123. }
  124.  
  125. void sortTable(team teams1[])
  126. {
  127. team temp[1];
  128. int i, j;
  129. for ( i = 0; i < 6; i++)
  130. {
  131. for ( j = 6; j >=0; j--)
  132. {
  133. if (teams1[j].points > teams1[(j-1)].points)
  134. {
  135. temp[1] = teams1[(j-1)];
  136. teams1[(j-1)] = teams1[j];
  137. teams1[j] = temp[1];
  138. }
  139. }
  140. }
  141. }
  142.  
  143. return 0;
  144. }
Reputation Points: 10
Solved Threads: 0
Light Poster
dinamit875 is offline Offline
48 posts
since Mar 2009
Nov 1st, 2009
0
Re: just a quick question here
Can you post the errors being reported. Thanks.
Reputation Points: 100
Solved Threads: 48
Posting Pro in Training
Grn Xtrm is offline Offline
495 posts
since Nov 2008
Nov 1st, 2009
0
Re: just a quick question here
this is updated code:
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <string>
  4. #include <stdio.h>
  5.  
  6. using namespace std;
  7.  
  8. struct TeamInfo
  9. {
  10. char name[50];
  11. int points;
  12. int goalsFor;
  13. int goalsAgainst;
  14. int played;
  15. int won;
  16. int lost;
  17. int drawn;
  18. };
  19. typedef struct team;
  20.  
  21. void addTeam(char[50], int);
  22. void calculateResult(int, int, int, int);
  23. void sortTable(team[]);
  24.  
  25. int main(void)
  26. {
  27.  
  28. char newTeam[50];
  29. int menuChoice = 0;
  30. int numOfTeams = 0;
  31. int team1, team2, team1Score, team2Score;
  32.  
  33. team teams[6];
  34.  
  35.  
  36. int i;
  37. while (menuChoice != 4)
  38. {
  39. cout << "Football League\n\n" << endl;
  40. cout << "1. Add team\n" << endl;
  41. cout << "2. Display league table\n" << endl;
  42. cout << "3. Add result\n" << endl;
  43. cout << "4. Quit\n" << endl;
  44. cin >> menuChoice;
  45.  
  46. if (menuChoice == 1)
  47. {
  48. if (numOfTeams == 6)
  49. cout << "Error: Maximum amount of teams has been entered" << endl;
  50. else
  51. {
  52. cout << "Add new team\n" << endl;
  53. cin >> newTeam, sizeof(newTeam), stdin;
  54. newTeam[strlen(newTeam)-1] = '\0';
  55. cin >> newTeam, sizeof(newTeam), stdin;
  56. newTeam[strlen(newTeam)-1] = '\0';
  57. addTeam(newTeam, numOfTeams);
  58. numOfTeams++;
  59. cout << "\nNew team added\n" << endl;
  60. }
  61. }
  62. else if (menuChoice == 2)
  63. {
  64. cout << "\t\tP\tW\tD\tL\tF\tA\tT\n\n\n" << endl;
  65. for ( i = 0; i < 6; i++)
  66. {
  67. cout << "%s\t\t%d\t%d\t%d\t%d\t%d\t%d\t%d\n",
  68. teams[i].name, teams[i].played, teams[i].won,teams[i].drawn,
  69. teams[i].lost, teams[i].goalsFor, teams[i].goalsAgainst, teams[i].points << endl;
  70. }
  71. }
  72. else if (menuChoice == 3)
  73. {
  74. cout << "Enter first team playing:\n" << endl;
  75. cin >> team1;
  76. cout << "Enter second team playing:\n" << endl;
  77. cin >> team2;
  78. cout << "Enter first teams score:\n" << endl;
  79. cin >> team1Score;
  80. cout << "Enter second teams score:\n" << endl;
  81. cin >> team2Score;
  82. calculateResult(team1, team2, team1Score, team2Score);
  83. }
  84. }
  85. void addTeam(char *teamName, int i)
  86. {
  87. strcpy(teams[i].name, teamName);
  88. teams[i].points = 0;
  89. teams[i].goalsFor = 0;
  90. teams[i].goalsAgainst = 0;
  91. teams[i].played = 0;
  92. teams[i].won = 0;
  93. teams[i].lost = 0;
  94. teams[i].drawn = 0;
  95. }
  96.  
  97. void calculateResult(int t1, int t2, int t1R, int t2R)
  98. {
  99. if (t1R > t2R)
  100. {
  101. teams[(t1-1)].points+=3;
  102. teams[(t1-1)].won++;
  103. teams[(t2-1)].lost++;
  104. }
  105. else if (t2R > t1R)
  106. {
  107. teams[(t2-1)].points+=3;
  108. teams[(t2-1)].won++;
  109. teams[(t1-1)].lost++;
  110. }
  111. else
  112. {
  113. teams[(t2-1)].points++;
  114. teams[(t1-1)].points++;
  115. teams[(t1-1)].drawn++;
  116. teams[(t2-1)].drawn++;
  117. }
  118. teams[(t1-1)].goalsFor+=t1R;
  119. teams[(t2-1)].goalsFor+=t2R;
  120. teams[(t1-1)].goalsAgainst+=t2R;
  121. teams[(t2-1)].goalsAgainst+=t1R;
  122. teams[(t1-1)].played++;
  123. teams[(t2-1)].played++;
  124.  
  125. sortTable(teams);
  126. }
  127.  
  128. void sortTable(team teams1[])
  129. {
  130. team temp[1];
  131. int i, j;
  132. for ( i = 0; i < 6; i++)
  133. {
  134. for ( j = 6; j >=0; j--)
  135. {
  136. if (teams1[j].points > teams1[(j-1)].points)
  137. {
  138. temp[1] = teams1[(j-1)];
  139. teams1[(j-1)] = teams1[j];
  140. teams1[j] = temp[1];
  141. }
  142. }
  143. }
  144. }
  145.  
  146. return 0;
  147. }
and these are the errors im gettin, look like it need a ";" or something like that???
In function `int main()':
33 elements of array `team teams[6]' have incomplete type In function `int main()':
33 storage size of `teams' isn't known
86 a function-definition is not allowed here before '{' token
86 expected `,' or `;' before '{' token
98 a function-definition is not allowed here before '{' token
98 expected `,' or `;' before '{' token
129 a function-definition is not allowed here before '{' token
129 expected `,' or `;' before '{' token
thanks
Reputation Points: 10
Solved Threads: 0
Light Poster
dinamit875 is offline Offline
48 posts
since Mar 2009
Nov 1st, 2009
0
Re: just a quick question here
You need to put return 0; at the end of main() and remove it from the end of the code. Remove the last 2 lines of the last code you posted.
Reputation Points: 100
Solved Threads: 48
Posting Pro in Training
Grn Xtrm is offline Offline
495 posts
since Nov 2008
Nov 1st, 2009
0
Re: just a quick question here
when im removing return 0 from the end and putting it after main(),it gives me more errors
Reputation Points: 10
Solved Threads: 0
Light Poster
dinamit875 is offline Offline
48 posts
since Mar 2009
Nov 1st, 2009
0
Re: just a quick question here
Don't put it after main() put it within main() just before the right brace that terminates main().
Reputation Points: 100
Solved Threads: 48
Posting Pro in Training
Grn Xtrm is offline Offline
495 posts
since Nov 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: C++ and CSV file
Next Thread in C++ Forum Timeline: Looping through the windows registry using RegEnumKeyEx till the leaves





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC