Thread: So close!

  1. #1
    Interested Newbie
    Join Date
    Sep 2004
    Location
    Sweden
    Posts
    51

    So close!

    Code:
    #include <cstdlib>
    #include <iostream>
    #include <stdio.h>
    #include <time.h>
    
    using namespace std;
    
    char yorn;
    
    void computer_win();
    void computer_lose();
    void computer_tie();
    
    void computer_win()
    {
    	cout << "Haha, datorn vann." << endl;
    	cout << "------------------" << endl;
    	cout << "Vill du försöka igen? [J]" << endl;	
    	cin >> yorn;
    }
    
    void computer_lose()
    {
    	cout << "Grattis! Du vann!" << endl;
    	cout << "-----------------" << endl;
    	cout << "Vill du försöka igen? [J]" << endl;	
    	cin >> yorn;
    }
    
    void computer_tie()
    {
    	cout << "Oavgjort." << endl;
    	cout << "---------" << endl;
    	cout << "Vill du försöka igen? [J]" << endl;	
    	cin >> yorn;
    }
    
    int main()
    {
    	srand(time(NULL));
    	char yorn;
    	int computer_attack = rand()%3+1;	
    	
    	do
    	{
    	//Val av attack
    	int input;
    	cout << "Vad vill du attackera med?" << endl;
    	cout << "1. Sten" << endl;
    	cout << "2. Sax" << endl;
    	cout << "3. Påse" << endl;
    	cin >> input;
    	cout << "Du har nu gjort ditt val." << endl;
    	cout << "-------------------------" << endl;
    
    	switch(computer_attack)
    	{
    	case 1:
    	cout << "Datorn valde Sten. "{ << endl;
    	
    	if(input = 1)	
    	{
    	computer_tie();
    	}
    	else if(input = 2)
    	{
    	computer_win();
    	}
    	else if(input = 3)
    	{
    	computer_lose();
    	}
    	break;
    	
    	case 2:
    	cout << "Datorn valde Sax. " << endl;
    	
    	if(input = 1)	
    	{
    	computer_lose();
    	}
    	else if(input = 2)
    	{
    	computer_tie();
    	}
    	else if(input = 3)
    	{
    	computer_win();
    	}
    	break;
    	
    	case 3:
    	cout << "Datorn valde Påse. " << endl;
    	
    	if(input = 1)	
    	{
    	computer_win();
    	}
    	else if(input = 2)
    	{
    	computer_lose();
    	}
    	else if(input = 3)
    	{
    	computer_tie();
    	}	
    	break;
    	
    	default:
    	cout << "Error" << endl;
    	}
    	}
    		while( yorn == 'j');
    	return 0;
    }
    And the errors/warnings:
    c:\documents and settings\jb04datynnh\mina dokument\visual studio projects\sten, sax, påse\source.cpp(40) : warning C4244: 'argument' : conversion from 'time_t' to 'unsigned int', possible loss of data
    c:\documents and settings\jb04datynnh\mina dokument\visual studio projects\sten, sax, påse\source.cpp(59) : error C2143: syntax error : missing ';' before '{'
    c:\documents and settings\jb04datynnh\mina dokument\visual studio projects\sten, sax, påse\source.cpp(59) : error C2143: syntax error : missing ';' before '<<'
    c:\documents and settings\jb04datynnh\mina dokument\visual studio projects\sten, sax, påse\source.cpp(116) : fatal error C1075: end of file found before the left brace '{' at 'c:\documents and settings\jb04datynnh\mina dokument\visual studio projects\sten, sax, påse\source.cpp(39)' was matched

    I feel I'm so close!

  2. #2
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    3 lines after switch you have
    cout << "Datorn valde Sten. "{ << endl; //remove the {


    close you switch with }



    don't be afraid of the tab key......go in as far as you have to

    Code:
    for()
    {
            if()
            {
                    while()
                    {
                           if()
                           {
                            }
                    }
             }
    
    }
    see? now you won't have that problem

  3. #3
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    also, if you have one line of code within an if you do not need the {}
    Code:
    if(x = b)
           i++;
    else 
        if(b = n)
                i--;
        else
                i = i * 50

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    #include <stdio.h>
    #include <time.h>
    Those are deprecated... replace with:

    Code:
    #include <cstdio>
    #include <ctime>
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    Interested Newbie
    Join Date
    Sep 2004
    Location
    Sweden
    Posts
    51
    okey thanks alot

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > if(b = n)
    This is assignment, use == for comparison
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Close an HTTPListenerResponse.OutputStreram
    By George2 in forum C# Programming
    Replies: 0
    Last Post: 04-23-2008, 11:00 PM
  2. User input to close window
    By NoFearXD in forum Windows Programming
    Replies: 2
    Last Post: 05-09-2007, 07:38 PM
  3. open() and close()
    By SoFarAway in forum C Programming
    Replies: 3
    Last Post: 04-08-2005, 01:16 PM
  4. XWindows- Close window event?
    By Exile in forum Linux Programming
    Replies: 8
    Last Post: 01-09-2005, 10:39 PM
  5. Ghost in the CD Drive
    By Natase in forum A Brief History of Cprogramming.com
    Replies: 17
    Last Post: 10-12-2001, 05:38 PM