hello, I am following a book and im stuck on this one.....(note it's tacky but just bear with me, im a newb)

#include <iostream.h>
int main()
{
    int RedSoxScore, YankeesScore;
    cout<<"Enter the score for the RedSox:";
    cin>> RedSoxScore;
    
    cout<<"Enter the score for the yankees:";
    cin>> YankeesScore;
    
    cout<<"\n";
    
    if (RedSoxScore > YankeesScore)
    cout<<"GO THE BLIMMIN REDSOX!!\n";
    
    if (YankeesScore > RedSoxScore )
    {
                     cout<<"WHOOPY GO THE YANKS!!!\n";
                     cout<<"Happy days in NYC!!!\n";
    }
    
    if (RedSoxScore == YankeesScore)
    {
                    cout<<"A tie?, nha cant be!\n";
                    cout<<" Give me the real score for the yanks:";
                    cin >> YankeesScore;
                    
                    if (RedSoxScore > YankeesScore)
                    cout<<"Knew it, Go the sox!!";
                    
                    if (YankeesScore > RedSoxScore)
                    cout<<"Knew it go the yanks!!";
                    
                    if (YankeesScore == RedSoxScore)
                    cout<<"Wow, it was tie!?";
    }
    
    cout<<"\nThanks for telling me.\n";
    return 0;
}

I know im using the old .h header files, because that's what the book uses (it says i can use the new standards if i want, using namespace std but my code tends to not work sometimes. So im sticking to the old ones just untill i get the hang of things.

Anyway i compiled the following on devc++ and it runs the first few line well untill the command promt suddently closes after line 9.

The code seems to be fine because it compiled, but it just flashes.

Any ideas as to what im doing wrong?....
-Thanks

Recommended Answers

All 5 Replies

Hi revenge2,

I looked in your code. It works as expected. I don't understand what do you mean by "flashing". You may like to add followin lines before return.
I am using Microsoft C++ therefore I used

_getch

you may use anything which blocks the execution and let your program eait for user input.

Final code looks like...

cout<<"Press any key to continue."
int i = _getch();
#include "conio.h"
#include<iostream>
using namespace std ;

int _tmain(int argc, _TCHAR* argv[])
{
	int RedSoxScore, YankeesScore;
	cout<<"Enter the score for the RedSox:";
	cin>> RedSoxScore;

	cout<<"Enter the score for the yankees:";
	cin>> YankeesScore;

	cout<<"\n";

	if (RedSoxScore > YankeesScore)
		cout<<"GO THE BLIMMIN REDSOX!!\n";

	if (YankeesScore > RedSoxScore )
	{
		cout<<"WHOOPY GO THE YANKS!!!\n";
		cout<<"Happy days in NYC!!!\n";
	}

	if (RedSoxScore == YankeesScore)
	{
		cout<<"A tie?, nha cant be!\n";
		cout<<" Give me the real score for the yanks:";
		cin >> YankeesScore;

		if (RedSoxScore > YankeesScore)
			cout<<"Knew it, Go the sox!!";

		if (YankeesScore > RedSoxScore)
			cout<<"Knew it go the yanks!!";

		if (YankeesScore == RedSoxScore)
			cout<<"Wow, it was tie!?";
	}
                cout<<"\nThanks for telling me.\n";

	cout<<"Press any key to continue."
	int i = _getch();
	return 0;
}

>>_getch_getch
This is a C++ program, not C. So don't use that C function. Use cin.ignore() or cin.get() so that you don't mix C and C++ in the same program. Sure of course getch() works, but its not consistent and you might get lower marks from your instructor for using it.

unfortunately i dont undertand that, ahh i have only been using iostream for the purpose of learning plus the book seems to use this for the time being. I dont have an instructor/teacher or anything im just trying to learn from home, after school.

Anyway is there anywway to dum it down a bit?! like explain what "cin.ignore()" does exactly-oh and please dont mix c with c++ i get even more lost (lol)

Flashing meaning; when i run the program and enter the score' at line 9 the command prompt just closes before i see or get to do anything.
-thanks

>>I dont have an instructor/teacher or anything im just trying to learn from home, after school.
Good luck. Please don't hesitate to ask questions here.

>>like explain what "cin.ignore()
See this link for some good info about cin

>>does exactly-oh and please dont mix c with c++ i
It means do not mix the two languages in the same program. C++ supports most C functions but it is not considered good programming practice to mix them up. Specifically -- don't use anything found in stdio.h in a c++ program. And don't use anything in conio.h at all because those functions are not standard, which means they may or may not be supported by a compiler.

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.