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.
dinamit875 0 Light Poster
William Hemsworth 1,339 Posting Virtuoso
Well, C++ can compile almost any C code, except for a few minors differences. Post the code and I'll see.
dinamit875 0 Light Poster
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++.
Grn Xtrm 84 Posting Pro in Training
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.
Edited by Grn Xtrm because: n/a
dinamit875 0 Light Poster
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;
}
Grn Xtrm 84 Posting Pro in Training
Can you post the errors being reported. Thanks.
dinamit875 0 Light Poster
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
Grn Xtrm 84 Posting Pro in Training
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.
dinamit875 0 Light Poster
when im removing return 0 from the end and putting it after main(),it gives me more errors:(:(
Grn Xtrm 84 Posting Pro in Training
Don't put it after main() put it within main() just before the right brace that terminates main().
dinamit875 0 Light Poster
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.
jonsca 1,059 Quantitative Phrenologist Team Colleague Featured Poster
typedef struct teams;
should be typedef struct TeamInfo teams;
Change your cout on line 67 above to cout <<variable1<<"\t\t"<<variable2<<"\t\t" etc. etc.
There are major issues with scope too but grab the basic stuff first.
Edited by jonsca because: n/a
dinamit875 0 Light Poster
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.
jonsca 1,059 Quantitative Phrenologist Team Colleague Featured Poster
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;
.
dinamit875 0 Light Poster
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.
jonsca 1,059 Quantitative Phrenologist Team Colleague Featured Poster
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 to team to avoid confusion.
Edited by jonsca because: n/a
dinamit875 0 Light Poster
yes,these changes are relevnt and i have made thembut still getting errors in void function, maybe problem is in the main()???
jonsca 1,059 Quantitative Phrenologist Team Colleague Featured Poster
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 teams*
(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.
Edited by mike_2000_17 because: Fixed formatting
dinamit875 0 Light Poster
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];
}
}
}
}
//}
jonsca 1,059 Quantitative Phrenologist Team Colleague Featured Poster
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).
dinamit875 0 Light Poster
Finally I got this program running, but it not working the way I was expecting it to work, can anyone run it and tell me what it wrong with it, please heres the full 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(T *,char[50],int);
//void addTeam(char[50], int);
void calculateResult(T *, 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(T, 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(T , team1, team2, team1Score, team2Score);
}
}
return 0;
}
void addTeam(T* T, char*teamName,int i)
//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(T* T, 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];
}
}
}
}
dinamit875 0 Light Poster
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 tovoid 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).
thanks mate, it helps :) could you have a look at the above post, the program runs, but its working differently.
jonsca 1,059 Quantitative Phrenologist Team Colleague Featured Poster
Like I had said before, don't make temp an array (it's got one element so what's the point?) just declare it T temp;
. Next, since you do not always have 6 teams, pass the number of teams into calculateResults (via an additional int parameter at the end) which can then be passed into sortTable. Make sure you also change the i<6
in your menu item 2 to i<numOfTeams (also like I had said before) so you don't overstep your array. Don't be afraid to absolutely pepper your code with cout statements to determine where things are crashing, it will help you in the long run.
dinamit875 0 Light Poster
Next, since you do not always have 6 teams, pass the number of teams into calculateResults (via an additional int parameter at the end) which can then be passed into sortTable.
I just dont get this part, the rest i have changed, you see when i am trying to add some teams it does not showing any results, i thing i have problems with this line, and have no clue how to change it: heres the piece of code:
else if (menuChoice == 2)
{
cout << "\t\tP\tW\tD\tL\tF\tA\tT\n\n\n" << endl;
for ( i = 0; i < numOfTeams; i++)
{
//i < numOfTeams;
cout << "%s\t\t%d\t%d\t%d\t%d\t%d\t%d\t%d\n" << endl; /////this line gives me all problems.
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;
}
}
jonsca 1,059 Quantitative Phrenologist Team Colleague Featured Poster
typedef struct teams;
should betypedef struct TeamInfo teams;
Change your cout on line 67 above to cout <<variable1<<"\t\t"<<variable2<<"\t\t" etc. etc.
That's what I meant by the above cout<<teams[i].name<< "\t\t" << ETC
http://publib.boulder.ibm.com/infocenter/comphelp/v101v121/index.jsp?topic=/com.ibm.xlcpp101.aix.doc/legacy/id00066.html Let the great Google be your guide. Read up on cout and cin if you're still having trouble, but the << and >> operator syntax is quite intuitive, cout << stuff
to send stuff to output, cin >> stuff
to send input from cin to stuff.
dinamit875 0 Light Poster
i got it now, i knew what you ment, but i wasnt sure what supposed to be instead of "variable1", etc, im goint to put it cout<<teams.name<< " played, goals for, againts, etc, hopefully it will work. thanks again.
dinamit875 0 Light Poster
well I did all of the above steps, the code is done the wright way, i hope, but still some unclear things are happening,such as, when i am adding teams and trying to display it, it show only one team at a time, i have to type in some digit in order to display another teams, why would that happen? also adding score is displaying some useless numbers, can anyone expalain this happening.thanks.
this is complete working 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(T *, char[50], int);
//void addTeam(char[50], int);
void calculateResult(T *, 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 << "*** SOCCER LEAGUE TABLE ***\n\n" << endl;
cout << "1. Add new team\n" << endl;
cout << "2. Display soccer league table\n" << endl;
cout << "3. Add result or score\n" << endl;
cout << "4. Quit the program\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(T, newTeam, numOfTeams);
//numOfTeams++;
cout << "\nNew team added\n" << endl;
system("pause");
system("CLS");
numOfTeams++;
}
}
else if (menuChoice == 2)
{
cout << "Team Name\tPlayed\tWon\tDraw\tLost\tFor\tAgainst\t\tTotal\n" << endl;
//cin.get();
for ( i = 0; i < numOfTeams; i++)
{
i == numOfTeams;
cout << T[i].name<<"\t\t"<<T[i].played<<"\t"<<T[i].won<<"\t"<<T[i].drawn<<"\t"<<T[i].lost<<"\t"<<T[i].goalsFor<<"\t"<<T[i].goalsAgainst<<"\t\t"<<T[i].points<<"\n\n\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;
//system("pause");
//cin.ignore();
}
}
else if (menuChoice == 3)
{
cout << "Enter first team playing the match:\n" << endl;
cin >> team1;
cout << "Enter second team playing the match:\n" << endl;
cin >> team2;
cout << "Enter first teams score:\n" << endl;
cin >> team1Score;
cout << "Enter second teams score:\n" << endl;
cin >> team2Score;
calculateResult(T , team1, team2, team1Score, team2Score);
}
}
return 0;
}
void addTeam(T* T, 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(T* T, int t1, int t2, int t1R, int t2R)//, int i[])
{
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);
int numOfTeams = 0;
}
void sortTable(T teams1[])
{
int numOfTeams = 0;
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];
}
}
}
}
BeyondTheEye 2 Junior Poster in Training
Change line 55 from cin >> newTeam, sizeof(newTeam), stdin;
to cin >> newteam;
Line 74 still has the same problem. You fixed your cout statement on the line above, but not the cin line. I'd follow jonsca's advice and read up on cin/cout..
Well, short version:
int a, b;
cin >> a >> b;
//and not cin >> a,b;
Also, you might want to look into standard strings, instead of cstrings. I'd first try to get the program to work correctly, but if you want C++ code you should look into it.
Edited by BeyondTheEye because: n/a
dinamit875 0 Light Poster
Change line 55 from
cin >> newTeam, sizeof(newTeam), stdin;
tocin >> newteam;
Line 74 still has the same problem. You fixed your cout statement on the line above, but not the cin line. I'd follow jonsca's advice and read up on cin/cout..Well, short version:
int a, b; cin >> a >> b; //and not cin >> a,b;
Also, you might want to look into standard strings, instead of cstrings. I'd first try to get the program to work correctly, but if you want C++ code you should look into it.
thanks for the quote.
I have made those changes but it didn't really helped, but still they were relevant, im just trying to find out ehat is wrong with add team function and add result function, because its not working properly.:(:(
jonsca 1,059 Quantitative Phrenologist Team Colleague Featured Poster
Next, since you do not always have 6 teams, pass the number of teams into calculateResults (via an additional int parameter at the end) which can then be passed into sortTable. Don't be afraid to absolutely pepper your code with cout statements to determine where things are crashing, it will help you in the long run.
Still holds. Don't hard-code 6 into your sortTable function. So, sortTable needs the number of teams. sortTable is called within calculateResults, which has no knowledge of numOfTeams either. How can we provide that knowledge? Pass numOfTeams into calculateResults as an extra parameter, then pass it into sortTable. I know you can do this. Go step by step. I cannot in good conscience keep translating these concepts into the code for you. This assignment is for your benefit.
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.