Hi!
My assignment is to write a program where you write in two sport teams, home teams and guest team and their reults. After the program will output information who won and if there were a tie. I tried to translate okay from Swedish!
The program isn't working and there are no error message.
Can someone help me!
CMD displays the first output, but after that the program shuts down with an error message.

#include <iostream>
#include <string>

 using namespace std;
  int main(void)
 {

	 string home, guest, hnr, bnr;

	 cout << "Title" << endl;
	 cout << "----------------" << endl;

	 cout << "Write name of home team: "; cin >> home;
	 cout << "How many goals did" + home; cin >> hnr;

	 cout << "Write name of guest team: "; cin >> guest;
	 cout << "How many goals did " + guest; cin >> bnr;

	 if (hnr > bnr)
	 {
		 cout << home << " won against " 
			  << guest << "The resultat were " 
			  << hnr << "-" << bnr;
	 }
	 else if (hnr < bnr)
	 {	
		cout << guest << " guest won over " 
			 << home << ". Final result " 
			 << hnr << "-" << bnr;
	 }
	 else (hnr && bnr);
	 {	
		 cout << Tie between " 
			  << home << " and " 
			  << guest << ". The match ended    with " 
			  << hnr << "-" << bnr;
	 }
  }
mvmalderen commented: if(post == 1 && used_code_tags) rep += 9; :P +9

Recommended Answers

All 45 Replies

You'r entering goals as strings but you aren't comparing the integer values of those strings.

commented: Indeed :) +9

You'r entering goals as strings but you aren't comparing the integer values of those strings.

Conclusion:
If you want to store numeric values it's better to use numeric datatypes as well, and not strings, in this case an integer would fit the best, you declare an integer like this:

int mynumber;

And you can use cin to get some data from the user into that variable, like this:

cin >> mynumber;

For storing the number of goals for 'home' and 'guest' you'll have to use integer variables then, so this line:

string home, guest, hnr, bnr;

has to be separated into two lines, one for the integer variables (for storing the number of goals), and one line where the definition of variables 'home' and 'guest' is on (for storing the team's names):

string home, guest;
int hnr, bnr; // declare integer variables

:)

You forget " before Tie between ".

And your If statement ... first time you are checking if hnr is greater, secondly if it is lesser, so for the third options nothing else left than for them to be equal. So remove (hnr && bnr) <- which is also incorrect...if you want to check if two values are equal you must use ==( two of them together ... one serves you to assign value to variable )

So your if statement would look like that:

if (hnr > bnr)
{
}
else if (hnr < bnr)
{
}
else
{	
}

Thank you for all help!

Is your problem solved?
Then please mark the thread as solved, this increases efficiency on the forum :)

I think I need more help!

My teacher wanted me to have more output situations.
Now my code look like this and again I don't get any errors, but the output is wrong!

#include <iostream>
#include <string>


 using namespace std;
  int main(void)
 {

	 string home, guest; 
                 int hnr, bnr;

	 cout << "Title" << endl;
	 cout << "----------------" << endl;

	 cout << "Name on home team: "; cin >> home;
	 cout << "How many goals for " << home << ":";              cin>>hnr;
	 cout << endl;
    
	 cout << "Name of guest team: "; cin >> guest;
	 cout << "How many goals for " << guest << ":"; cin >>     bnr;
	 cout <<endl;
   


	 if (hnr > bnr)
	 {
		  if (hnr > 3 && bnr < 3)
			 cout << "Home team crushed guest  team";
		  else
				cout << home << " won home against " 
				<< guest << ". Final result " 
				 << hnr << "-" << bnr;
		
	 }
	 else if (bnr < hnr)
	 {	
		  if (bnr > 3 && hnr < 3)
			  cout << "Guest team crushed home team";
		  else
       		  cout << guest << " guest won against " 
			 << home << ". Final result " 
			 << hnr << "-" << bnr;
	 }
	 else (bnr == hnr);
		 cout << "Tie between " 
			  << home << " and " 
			  << guest << ". Final result " 
              << hnr << "-" << bnr; 
  }

This: [B]else [B](bnr == hnr)[/B][B];[/B][/B] is not correct, remove the semicolon ( ; ) and (bnr == hnr) so that it becomes else

It doesnt work anyway!
I don't understand!
Can someone try to debug it?

Post your current code after the edit's which Tux4Life told you to make.

You've a strange way to implement such a simple thing:
Change:

if (hnr > bnr)
	 {
		  if (hnr > 3 && bnr < 3)
			 cout << "Home team crushed guest  team";
		  else
				cout << home << " won home against " 
				<< guest << ". Final result " 
				 << hnr << "-" << bnr;
		
	 }
	 else if (bnr < hnr)
	 {	
		  if (bnr > 3 && hnr < 3)
			  cout << "Guest team crushed home team";
		  else
       		  cout << guest << " guest won against " 
			 << home << ". Final result " 
			 << hnr << "-" << bnr;
	 }
	 else (bnr == hnr);
		 cout << "Tie between " 
			  << home << " and " 
			  << guest << ". Final result " 
              << hnr << "-" << bnr; 
  }

To something like this:

if(hnr > bnr) {
        cout << "Home wins!" << endl;
} else if (hnr < bnr) {
        cout << "Guest wins!" << endl;
} else {
        cout << "It's a tie!" << endl;
}

Is that what you intended your code to do?

Yes, from the beginning, but my teacher wanted me to add more situations!

For example:
"Home team crusched guest with 5-1!"
Vice versa!

Yes, from the beginning, but my teacher wanted me to add more situations!

For example:
"Home team crusched guest with 5-1!"
Vice versa!

Well, that's not so difficult, give it a try (starting from the corrected code), in that case it's easier to track down any future problems :)
You only have to add an extra cout statement ...

No, I need to add more if-situations!
Do you understand what I mean? My english isn't the best!

No, I need to add more if-situations!
Do you understand what I mean? My english isn't the best!

Yes, I understand what you mean, could you please post your whole assignment, in that way I can better understand the problem you're having :)

This it the following if-situations I need:

If home team won over guest team
If home team won ridiculously big over guest team
If guest team won over home team
If guest team won ridiculously big over home tam
If they got the same score

Firstly You should define, what Ridiculously Big Means,

I see that you have already coded the whole part out, Post your code and tell where you are having problems?

>This it the following if-situations I need:
>
>If home team won over guest team <implemented>
>If home team won ridiculously big over guest team <unimplemented>
>If guest team won over home team <implemented>
>If guest team won ridiculously big over home tam <unimplemented>
>If they got the same score <implemented>

Could you define "ridiculously big" ?

Your assignment is to make a program which automatice the output of an sportstitel!
The user inputs the team name and score of the home and guest team. After the program will automaticly write a fitting sentence for the situation.

Your assignment is to make a program which automatice the output of an sportstitel!
The user inputs the team name and score of the home and guest team. After the program will automaticly write a fitting sentence for the situation.

That part is already implemented, it's working correctly, let's move on to the unimplemented parts:

  • If home team won ridiculously big over guest team <unimplemented>
  • If guest team won ridiculously big over home tam <unimplemented>

:)

I think my teacher means that ridicolously big is 1-5, 3-7 and so on, instead of 1-2 or 2-3!

Hey , Post down your code,

According to me, You have completed your project, The only problems you might have is some syntax errors, Apart from that you shouldn't have any major problem.

Excuse me.. I don't understand!
It's the first week for me!
I feel ridiculously stupid! ; )

Excuse me.. I don't understand!
It's the first week for me!
I feel ridiculously stupid! ; )

Never mind, we'll be glad to help you out, just post your current code and we'll take a look :)

This is the final code, and it doesn't work when I run it, the answers are wrong i CMD!

#include <iostream>
#include <string>


 using namespace std;
  int main(void)
 {

	 string home, guest; 
                 int hnr, bnr;

	 cout << "Title" << endl;
	 cout << "----------------" << endl;

	 cout << "Name on home team: "; cin >> home;
	 cout << "How many goals for " << home << ":";              cin>>hnr;
	 cout << endl;
    
	 cout << "Name of guest team: "; cin >> guest;
	 cout << "How many goals for " << guest << ":"; cin >>     bnr;
	 cout <<endl;
   


	 if (hnr > bnr)
	 {
		  if (hnr > 3 && bnr < 3)
			 cout << "Home team crushed guest  team";
		  else
				cout << home << " won home against " 
				<< guest << ". Final result " 
				 << hnr << "-" << bnr;
		
	 }
	 else if (bnr < hnr)
	 {	
		  if (bnr > 3 && hnr < 3)
			  cout << "Guest team crushed home team";
		  else
       		  cout << guest << " guest won against " 
			 << home << ". Final result " 
			 << hnr << "-" << bnr;
	 }
	 else (bnr == hnr);
		 cout << "Tie between " 
			  << home << " and " 
			  << guest << ". Final result " 
              << hnr << "-" << bnr; 
  }#include <iostream>
#include <string>


 using namespace std;
  int main(void)
 {

	 string home, guest; 
                 int hnr, bnr;

	 cout << "Title" << endl;
	 cout << "----------------" << endl;

	 cout << "Name on home team: "; cin >> home;
	 cout << "How many goals for " << home << ":";              cin>>hnr;
	 cout << endl;
    
	 cout << "Name of guest team: "; cin >> guest;
	 cout << "How many goals for " << guest << ":"; cin >>     bnr;
	 cout <<endl;
   


	 if (hnr > bnr)
	 {
		  if (hnr > 3 && bnr < 3)
			 cout << "Home team crushed guest  team";
		  else
				cout << home << " won home against " 
				<< guest << ". Final result " 
				 << hnr << "-" << bnr;
		
	 }
	 else if (bnr < hnr)
	 {	
		  if (bnr > 3 && hnr < 3)
			  cout << "Guest team crushed home team";
		  else
       		  cout << guest << " guest won against " 
			 << home << ". Final result " 
			 << hnr << "-" << bnr;
	 }
	 else (bnr == hnr)
		 cout << "Tie between " 
			  << home << " and " 
			  << guest << ". Final result " 
              << hnr << "-" << bnr; 
  }

[B]else if (bnr < hnr)[/B] has to be else if (bnr > hnr) :)

Otherwise you've two times the same: if (hnr > bnr) and else if (bnr < hnr) are in my eyes exactly the same, just another way of writing :)

And change else (bnr == hnr); to else if(bnr == hnr) So your fixed code would become:

#include <iostream>
#include <string>


 using namespace std;
  int main(void)
 {

	 string home, guest; 
                 int hnr, bnr;

	 cout << "Title" << endl;
	 cout << "----------------" << endl;

	 cout << "Name on home team: "; cin >> home;
	 cout << "How many goals for " << home << ":";              cin>>hnr;
	 cout << endl;
    
	 cout << "Name of guest team: "; cin >> guest;
	 cout << "How many goals for " << guest << ":"; cin >>     bnr;
	 cout <<endl;
   


	 if (hnr > bnr)
	 {
		  if (hnr > 3 && bnr < 3)
			 cout << "Home team crushed guest  team";
		  else
				cout << home << " won home against " 
				<< guest << ". Final result " 
				 << hnr << "-" << bnr;
		
	 }
	 else if (bnr [B]>[/B] hnr) [B]// changes made here[/B]
	 {	
		  if (bnr > 3 && hnr < 3)
			  cout << "Guest team crushed home team";
		  else
       		  cout << guest << " guest won against " 
			 << home << ". Final result " 
			 << hnr << "-" << bnr;
	 }
	 else [B]if[/B](bnr == hnr) [B]// changes made here[/B]
		 cout << "Tie between " 
			  << home << " and " 
			  << guest << ". Final result " 
              << hnr << "-" << bnr; 
  }

Thank you for taking your time!
I tried to run it again, but it still doesnt show the right output!

Thank you for taking your time!
I tried to run it again, but it still doesnt show the right output!

What values are you entering, what output do you get and what's the expected output?

When home team is 7 and guest team is 3 the CMD shuts down!
Thats my new problem!

= )

When home team is 7 and guest team is 3 the CMD shuts down!
Thats my new problem!

= )

There's nothing wrong with your program, with me it just runs fine, this is the output I'm getting:

Title
----------------
Name on home team: MyHomeTeam
How many goals for MyHomeTeam:7

Name of guest team: MyGuestTeam
How many goals for MyGuestTeam:3

MyHomeTeam won home against MyGuestTeam. Final result 7-3

Make the latest line of your main function: [B]cin.get();[/B] and the problem will be solved :)

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.