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.
Well, C++ can compile almost any C code, except for a few minors differences. Post the code and I'll see.
I know that c++ can compile most of c code, but i just need c++ code not c code heres the code:
#include <stdio.h>
#include <string.h>
void addTeam(char[50], int);
void calculateResult(int, int, int, int);
char newTeam[50];
int menuChoice = 0;
int numOfTeams = 0;
int team1, team2, team1Score, team2Score;
typedef struct
{
char name[50];
int points, goalsFor, goalsAgainst, played, won, lost, drawn;
} team;
team teams[6];
void sortTable(team[]);
int main(void)
{
int i;
while (menuChoice != 4)
{
printf("Football League\n\n");
printf("1. Add team\n");
printf("2. Display league table\n");
printf("3. Add result\n");
printf("4. Quit\n");
scanf("%d", &menuChoice);
if (menuChoice == 1)
{
if (numOfTeams == 6)
printf("Error: Maximum amount of teams has been entered");
else
{
printf("Add new team\n");
fgets(newTeam, sizeof(newTeam), stdin);
newTeam[strlen(newTeam)-1] = '\0';
fgets(newTeam, sizeof(newTeam), stdin);
newTeam[strlen(newTeam)-1] = '\0';
addTeam(newTeam, numOfTeams);
numOfTeams++;
printf("\nNew team added\n");
}
}
else if (menuChoice == 2)
{
printf("\t\tP\tW\tD\tL\tF\tA\tT\n\n\n");
for ( i = 0; i < 6; i++)
{
printf("%s\t\t%d\t%d\t%d\t%d\t%d\t%d\t%d\n",
teams[i].name, teams[i].played, teams[i].won,teams[i].drawn,
teams[i].lost, teams[i].goalsFor, teams[i].goalsAgainst,
teams[i].points);
}
}
else if (menuChoice == 3)
{
printf("Enter first team playing:\n");
scanf("%d", &team1);
printf("Enter second team playing:\n");
scanf("%d", &team2);
printf("Enter first teams score:\n");
scanf("%d", &team1Score);
printf("Enter second teams score:\n");
scanf("%d", &team2Score);
calculateResult(team1, team2, team1Score, team2Score);
}
}
return 0;
}
void addTeam(char *teamName, int i)
{
strcpy(teams[i].name, teamName);
teams[i].points = 0;
teams[i].goalsFor = 0;
teams[i].goalsAgainst = 0;
teams[i].played = 0;
teams[i].won = 0;
teams[i].lost = 0;
teams[i].drawn = 0;
}
void calculateResult(int t1, int t2, int t1R, int t2R)
{
if (t1R > t2R)
{
teams[(t1-1)].points+=3;
teams[(t1-1)].won++;
teams[(t2-1)].lost++;
}
else if (t2R > t1R)
{
teams[(t2-1)].points+=3;
teams[(t2-1)].won++;
teams[(t1-1)].lost++;
}
else
{
teams[(t2-1)].points++;
teams[(t1-1)].points++;
teams[(t1-1)].drawn++;
teams[(t2-1)].drawn++;
}
teams[(t1-1)].goalsFor+=t1R;
teams[(t2-1)].goalsFor+=t2R;
teams[(t1-1)].goalsAgainst+=t2R;
teams[(t2-1)].goalsAgainst+=t1R;
teams[(t1-1)].played++;
teams[(t2-1)].played++;
sortTable(teams);
}
void sortTable(team teams1[])
{
team temp[1];
int i, j;
for ( i = 0; i < 6; i++)
{
for ( j = 6; j >=0; j--)
{
if (teams1[j].points > teams1[(j-1)].points)
{
temp[1] = teams1[(j-1)];
teams1[(j-1)] = teams1[j];
teams1[j] = temp[1];
}
}
}
} I am trying to rewrite it into c++, but nothing good yet, i need to have same functions ect in c++.
Just look up printing and getting input using cout<< and cin>>. Also, use
#include <iostream>
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.
i have made those change to c code but getting another errors and here's the result:
//#include <string.h>
#include <iostream>
#include <iomanip>
#include <string>
#include <stdio.h>
using namespace std;
struct TeamInfo
{
char name[50];
int points, goalsFor, goalsAgainst, played, won, lost, drawn;
}
typedef struct team;
void addTeam(char[50], int);
void calculateResult(int, int, int, int);
void sortTable(team[]);
//sortTable(team, teams1[])
int main(void)
{
char newTeam[50];
int menuChoice = 0;
int numOfTeams = 0;
int team1, team2, team1Score, team2Score;
team teams[6];
int i;
while (menuChoice != 4)
{
cout << "Football League\n\n" << endl;
cout << "1. Add team\n" << endl;
cout << "2. Display league table\n" << endl;
cout << "3. Add result\n" << endl;
cout << "4. Quit\n" << endl;
cin >> menuChoice;
if (menuChoice == 1)
{
if (numOfTeams == 6)
cout << "Error: Maximum amount of teams has been entered" << endl;
else
{
cout << "Add new team\n" << endl;
cin.get(newTeam, sizeof(newTeam), stdin);
newTeam[strlen(newTeam)-1] = '\0';
cin.get(newTeam, sizeof(newTeam), stdin);
newTeam[strlen(newTeam)-1] = '\0';
addTeam(newTeam, numOfTeams);
numOfTeams++;
cout << "\nNew team added\n" << endl;
}
}
else if (menuChoice == 2)
{
cout << "\t\tP\tW\tD\tL\tF\tA\tT\n\n\n" << endl;
for ( i = 0; i < 6; i++)
{
cout << "%s\t\t%d\t%d\t%d\t%d\t%d\t%d\t%d\n",
teams[i].name, teams[i].played, teams[i].won,teams[i].drawn,
teams[i].lost, teams[i].goalsFor, teams[i].goalsAgainst, teams[i].points << endl;
}
}
else if (menuChoice == 3)
{
cout << "Enter first team playing:\n" << endl;
cin >> team1;
cout << "Enter second team playing:\n" << endl;
cin >> team2;
cout << "Enter first teams score:\n" << endl;
cin >> team1Score;
cout << "Enter second teams score:\n" << endl;
cin >> team2Score;
calculateResult(team1, team2, team1Score, team2Score);
}
}
void addTeam(char *teamName, int i)
{
strcpy(teams[i].name, teamName);
teams[i].points = 0;
teams[i].goalsFor = 0;
teams[i].goalsAgainst = 0;
teams[i].played = 0;
teams[i].won = 0;
teams[i].lost = 0;
teams[i].drawn = 0;
}
void calculateResult(int t1, int t2, int t1R, int t2R)
{
if (t1R > t2R)
{
teams[(t1-1)].points+=3;
teams[(t1-1)].won++;
teams[(t2-1)].lost++;
}
else if (t2R > t1R)
{
teams[(t2-1)].points+=3;
teams[(t2-1)].won++;
teams[(t1-1)].lost++;
}
else
{
teams[(t2-1)].points++;
teams[(t1-1)].points++;
teams[(t1-1)].drawn++;
teams[(t2-1)].drawn++;
}
teams[(t1-1)].goalsFor+=t1R;
teams[(t2-1)].goalsFor+=t2R;
teams[(t1-1)].goalsAgainst+=t2R;
teams[(t2-1)].goalsAgainst+=t1R;
teams[(t1-1)].played++;
teams[(t2-1)].played++;
sortTable(teams);
}
void sortTable(team teams1[])
{
team temp[1];
int i, j;
for ( i = 0; i < 6; i++)
{
for ( j = 6; j >=0; j--)
{
if (teams1[j].points > teams1[(j-1)].points)
{
temp[1] = teams1[(j-1)];
teams1[(j-1)] = teams1[j];
teams1[j] = temp[1];
}
}
}
}
return 0;
}this is updated code:
#include <iostream>
#include <iomanip>
#include <string>
#include <stdio.h>
using namespace std;
struct TeamInfo
{
char name[50];
int points;
int goalsFor;
int goalsAgainst;
int played;
int won;
int lost;
int drawn;
};
typedef struct team;
void addTeam(char[50], int);
void calculateResult(int, int, int, int);
void sortTable(team[]);
int main(void)
{
char newTeam[50];
int menuChoice = 0;
int numOfTeams = 0;
int team1, team2, team1Score, team2Score;
team teams[6];
int i;
while (menuChoice != 4)
{
cout << "Football League\n\n" << endl;
cout << "1. Add team\n" << endl;
cout << "2. Display league table\n" << endl;
cout << "3. Add result\n" << endl;
cout << "4. Quit\n" << endl;
cin >> menuChoice;
if (menuChoice == 1)
{
if (numOfTeams == 6)
cout << "Error: Maximum amount of teams has been entered" << endl;
else
{
cout << "Add new team\n" << endl;
cin >> newTeam, sizeof(newTeam), stdin;
newTeam[strlen(newTeam)-1] = '\0';
cin >> newTeam, sizeof(newTeam), stdin;
newTeam[strlen(newTeam)-1] = '\0';
addTeam(newTeam, numOfTeams);
numOfTeams++;
cout << "\nNew team added\n" << endl;
}
}
else if (menuChoice == 2)
{
cout << "\t\tP\tW\tD\tL\tF\tA\tT\n\n\n" << endl;
for ( i = 0; i < 6; i++)
{
cout << "%s\t\t%d\t%d\t%d\t%d\t%d\t%d\t%d\n",
teams[i].name, teams[i].played, teams[i].won,teams[i].drawn,
teams[i].lost, teams[i].goalsFor, teams[i].goalsAgainst, teams[i].points << endl;
}
}
else if (menuChoice == 3)
{
cout << "Enter first team playing:\n" << endl;
cin >> team1;
cout << "Enter second team playing:\n" << endl;
cin >> team2;
cout << "Enter first teams score:\n" << endl;
cin >> team1Score;
cout << "Enter second teams score:\n" << endl;
cin >> team2Score;
calculateResult(team1, team2, team1Score, team2Score);
}
}
void addTeam(char *teamName, int i)
{
strcpy(teams[i].name, teamName);
teams[i].points = 0;
teams[i].goalsFor = 0;
teams[i].goalsAgainst = 0;
teams[i].played = 0;
teams[i].won = 0;
teams[i].lost = 0;
teams[i].drawn = 0;
}
void calculateResult(int t1, int t2, int t1R, int t2R)
{
if (t1R > t2R)
{
teams[(t1-1)].points+=3;
teams[(t1-1)].won++;
teams[(t2-1)].lost++;
}
else if (t2R > t1R)
{
teams[(t2-1)].points+=3;
teams[(t2-1)].won++;
teams[(t1-1)].lost++;
}
else
{
teams[(t2-1)].points++;
teams[(t1-1)].points++;
teams[(t1-1)].drawn++;
teams[(t2-1)].drawn++;
}
teams[(t1-1)].goalsFor+=t1R;
teams[(t2-1)].goalsFor+=t2R;
teams[(t1-1)].goalsAgainst+=t2R;
teams[(t2-1)].goalsAgainst+=t1R;
teams[(t1-1)].played++;
teams[(t2-1)].played++;
sortTable(teams);
}
void sortTable(team teams1[])
{
team temp[1];
int i, j;
for ( i = 0; i < 6; i++)
{
for ( j = 6; j >=0; j--)
{
if (teams1[j].points > teams1[(j-1)].points)
{
temp[1] = teams1[(j-1)];
teams1[(j-1)] = teams1[j];
teams1[j] = temp[1];
}
}
}
}
return 0;
} 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
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.
when im removing return 0 from the end and putting it after main(),it gives me more errors:(:(
Don't put it after main() put it within main() just before the right brace that terminates main().
i know where to put it:):), but still some error come up here the code:
#include <iostream>
#include <iomanip>
#include <string>
#include <stdio.h>
using namespace std;
struct TeamInfo
{
char name[50];
int points;
int goalsFor;
int goalsAgainst;
int played;
int won;
int lost;
int drawn;
};
typedef struct teams;
void addTeam(char[50], int);
void calculateResult(int, int, int, int);
void sortTable(teams[]);
int main(void)
{
char newTeam[50];
int menuChoice = 0;
int numOfTeams = 0;
int team1, team2, team1Score, team2Score;
teams teams[6];
int i;
while (menuChoice != 4)
{
cout << "Football League\n\n" << endl;
cout << "1. Add team\n" << endl;
cout << "2. Display league table\n" << endl;
cout << "3. Add result\n" << endl;
cout << "4. Quit\n" << endl;
cin >> menuChoice;
if (menuChoice == 1)
{
if (numOfTeams == 6)
cout << "Error: Maximum amount of teams has been entered" << endl;
else
{
cout << "Add new team\n" << endl;
cin >> newTeam, sizeof(newTeam), stdin;
newTeam[strlen(newTeam)-1] = '\0';
cin >> newTeam, sizeof(newTeam), stdin;
newTeam[strlen(newTeam)-1] = '\0';
addTeam(newTeam, numOfTeams);
numOfTeams++;
cout << "\nNew team added\n" << endl;
}
}
else if (menuChoice == 2)
{
cout << "\t\tP\tW\tD\tL\tF\tA\tT\n\n\n" << endl;
for ( i = 0; i < 6; i++)
{
cout << "%s\t\t%d\t%d\t%d\t%d\t%d\t%d\t%d\n",
teams[i].name, teams[i].played, teams[i].won,teams[i].drawn,
teams[i].lost, teams[i].goalsFor, teams[i].goalsAgainst, teams[i].points << endl;
}
}
else if (menuChoice == 3)
{
cout << "Enter first team playing:\n" << endl;
cin >> team1;
cout << "Enter second team playing:\n" << endl;
cin >> team2;
cout << "Enter first teams score:\n" << endl;
cin >> team1Score;
cout << "Enter second teams score:\n" << endl;
cin >> team2Score;
calculateResult(team1, team2, team1Score, team2Score);
}
}
return 0;
}
void addTeam(char *teamName, int i)
{
strcpy(teams[i].name, teamName);
teams[i].points = 0;
teams[i].goalsFor = 0;
teams[i].goalsAgainst = 0;
teams[i].played = 0;
teams[i].won = 0;
teams[i].lost = 0;
teams[i].drawn = 0;
}
void calculateResult(int t1, int t2, int t1R, int t2R)
{
if (t1R > t2R)
{
teams[(t1-1)].points+=3;
teams[(t1-1)].won++;
teams[(t2-1)].lost++;
}
else if (t2R > t1R)
{
teams[(t2-1)].points+=3;
teams[(t2-1)].won++;
teams[(t1-1)].lost++;
}
else
{
teams[(t2-1)].points++;
teams[(t1-1)].points++;
teams[(t1-1)].drawn++;
teams[(t2-1)].drawn++;
}
teams[(t1-1)].goalsFor+=t1R;
teams[(t2-1)].goalsFor+=t2R;
teams[(t1-1)].goalsAgainst+=t2R;
teams[(t2-1)].goalsAgainst+=t1R;
teams[(t1-1)].played++;
teams[(t2-1)].played++;
sortTable(teams);
}
void sortTable(teams teams1[])
{
team temp[1];
int i, j;
for ( i = 0; i < 6; i++)
{
for ( j = 6; j >=0; j--)
{
if (teams1[j].points > teams1[(j-1)].points)
{
temp[1] = teams1[(j-1)];
teams1[(j-1)] = teams1[j];
teams1[j] = temp[1];
}
}
}
} i tryed to put in ";" after void function,but some errors are still there, if you have dev c++ compiler or anything similar please, try may be you will be able to fix them. thanks.
typedef struct teams; should be typedef struct TeamInfo teams;
Change your cout on line 67 above to cout <
well i have made those changes, but having problems with:
void addTeam(char *teamName, int i)
and
void calculateResult(int t1, int t2, int t1R, int t2R)
the error is as follows:
90-129 lines expected primary-expression before '[' token
can anyone fix this problem???please.
teams is not in scope for either of them. The functions have their own local scope and can't "see" the variables in main(). So, pass the array of structs into each of those, or I suppose you could use your function to return a new struct to be placed into the teams array.
Also, I noticed it before but cin >> newTeam, sizeof(newTeam), stdin; is probably not going to give you what you want, you don't need to specify those values, just take the input into newTeam with cin >>newTeam; .
teams is not in scope for either of them. The functions have their own local scope and can't "see" the variables in main(). So, pass the array of structs into each of those, or I suppose you could use your function to return a new struct to be placed into the teams array.
Also, I noticed it before but
cin >> newTeam, sizeof(newTeam), stdin;is probably not going to give you what you want, you don't need to specify those values, just take the input into newTeam withcin >>newTeam;.
I know what you mean, but i am not sure how would I imlement this in the code itself?could you please sypply me with the correct code, thanks.
Couple of other little issues: You prompt for a new team twice
In your calculate results you 1.) declare a 1 element array(??) and 2.) you reference it's one element by index 1.
In main change your for loop
else if (menuChoice == 2)
{
cout << "\t\tP\tW\tD\tL\tF\tA\tT\n\n\n" << endl;
for ( i = 0; i < 6; i++)
to i < numOfTeams
And change newTeam[strlen(newTeam)-1] = '\0'; to newTeam[strlen(newTeam)] = '\0'; otherwise you are truncating your string
As for the other part, add a parameter teams * teams I renamed it to teams * team and changed all your teams[i] to team[i] to avoid confusion.
yes,these changes are relevnt and i have made thembut still getting errors in void function, maybe problem is in the main()???
yes,these changes are relevnt and i have made thembut still getting errors in void function, maybe problem is in the main()???
Your array teams does not exist in this function. In order to use it you must pass it in. Give your function (both functions, really) a parameter called teams for it to be available in your function body. The type of teams the array is[icode] teams* [/code] (not to match your array in your function, but to match your typedef up above). In main, you need to change your function calls to reflect the new parameter (which you can pass in as teams, to match your array name) e.g., addTeam(teams,newTeam, numOfTeams);
For your own sanity, you should probably change the name of the typedef or the name of the array.
this is the code:
#include <iostream>
#include <iomanip>
#include <string>
#include <stdio.h>
using namespace std;
struct TeamInfo
{
char name[50];
int points;
int goalsFor;
int goalsAgainst;
int played;
int won;
int lost;
int drawn;
};
typedef struct TeamInfo T;
void addTeam(char[50], int);
void calculateResult(int, int, int, int);
void sortTable(T[]);
int main(void)
{
char newTeam[50];
int menuChoice = 0;
int numOfTeams = 0;
int team1, team2, team1Score, team2Score;
TeamInfo T[6];
int i;
while (menuChoice != 4)
{
cout << "Football League\n\n" << endl;
cout << "1. Add team\n" << endl;
cout << "2. Display league table\n" << endl;
cout << "3. Add result\n" << endl;
cout << "4. Quit\n" << endl;
cin >> menuChoice;
if (menuChoice == 1)
{
if (numOfTeams == 6)
cout << "Error: Maximum amount of teams has been entered" << endl;
else
{
cout << "Add new team\n" << endl;
cin >> newTeam;//, sizeof(newTeam), stdin;
newTeam[strlen(newTeam)] = '\0';
cin >> newTeam;//, sizeof(newTeam), stdin;
newTeam[strlen(newTeam)] = '\0';
addTeam(newTeam, numOfTeams);
numOfTeams++;
cout << "\nNew team added\n" << endl;
}
}
else if (menuChoice == 2)
{
cout << "\t\tP\tW\tD\tL\tF\tA\tT\n\n\n" << endl;
for ( i = 0; i < 6; i++)
{
//i < numOfTeams;
cout << "%s\t\t%d\t%d\t%d\t%d\t%d\t%d\t%d\n" << endl;
cin >> T[i].name, T[i].played, T[i].won, T[i].drawn, T[i].lost, T[i].goalsFor, T[i].goalsAgainst, T[i].points;
}
}
else if (menuChoice == 3)
{
cout << "Enter first team playing:\n" << endl;
cin >> team1;
cout << "Enter second team playing:\n" << endl;
cin >> team2;
cout << "Enter first teams score:\n" << endl;
cin >> team1Score;
cout << "Enter second teams score:\n" << endl;
cin >> team2Score;
calculateResult(team1, team2, team1Score, team2Score);
}
}
return 0;
}
//int void()
//{
void addTeam(char *teamName, int i)
{
strcpy(T[i].name, teamName);
T[i].points = 0;
T[i].goalsFor = 0;
T[i].goalsAgainst = 0;
T[i].played = 0;
T[i].won = 0;
T[i].lost = 0;
T[i].drawn = 0;
}
void calculateResult(int t1, int t2, int t1R, int t2R);
{
if (t1R > t2R)
{
T[(t1-1)].points+=3;
T[(t1-1)].won++;
T[(t2-1)].lost++;
}
else if (t2R > t1R)
{
T[(t2-1)].points+=3;
T[(t2-1)].won++;
T[(t1-1)].lost++;
}
else
{
T[(t2-1)].points++;
T[(t1-1)].points++;
T[(t1-1)].drawn++;
T[(t2-1)].drawn++;
}
T[(t1-1)].goalsFor+=t1R;
T[(t2-1)].goalsFor+=t2R;
T[(t1-1)].goalsAgainst+=t2R;
T[(t2-1)].goalsAgainst+=t1R;
T[(t1-1)].played++;
T[(t2-1)].played++;
sortTable(T);
}
void sortTable(T teams1[])
{
T temp[1];
int i, j;
for ( i = 0; i < 6; i++)
{
for ( j = 6; j >=0; j--)
{
if (teams1[j].points > teams1[(j-1)].points)
{
temp[1] = teams1[(j-1)];
teams1[(j-1)] = teams1[j];
teams1[j] = temp[1];
}
}
}
}
//}You should change teams to something else for EITHER the typedef OR the variable. But leave it as it is for now, and change the declaration of addteam to void addTeam(T *,char[50],int) and the definition to void addTeam(T* T, char*teamName,int i) (first T is for the typedef,*,second T for your local variable in your function). Now, your variable T is within the scope of the function (when you have a free minute reread that section of your text on scope because it's crucial to know).