Wanna Optimize my code?

This is a discussion on Wanna Optimize my code? within the C++ Programming forums, part of the General Programming Boards category; Hi! This is one of my first programs, and My first thread. I was wondering if anyone could tell why, ...

  1. #1
    Programmer
    Join Date
    Aug 2005
    Location
    Ohio
    Posts
    8

    Question Closing Problem!

    Hi! This is one of my first programs, and My first thread. I was wondering if anyone could tell why, after you miss the missing number twice, the rogram closes, and if there are any ways to optimize this code?
    Code:
    #include <cstdlib>
    #include <iostream>
    #include <stdlib.h>
    #include "stdio.h"
    #include "windows.h"
    
    using namespace std;
    
    int title()
    {
    cout << "W";
    Sleep(200);
    cout<< "e";
    Sleep(200);
    cout<<"l";
    Sleep(200);
    cout<< "c";
    Sleep(200);
    cout <<"o";
    Sleep(200);
    cout <<"m";
    Sleep(200);
    cout <<"e";
    Sleep(200);
    cout <<" ";
    Sleep(200);
    cout <<"t";
    Sleep(200);
    cout <<"o";
    Sleep(200);
    cout <<" ";
    cout <<".";
    Sleep(200);
    cout << ".";
    Sleep(200);
    cout << ".";
    Sleep(2000);
    cout <<"T\n";
    Sleep(200);
    cout <<"               h\n";
    Sleep(200);
    cout <<"                e\n";
    Sleep(200);
    cout <<"                  \n";
    Sleep(200);
    cout <<"                  G\n";
    Sleep(200);
    cout <<"                   u\n";
    Sleep(200);
    cout <<"                    e\n";
    Sleep(200);
    cout <<"                     s\n";
    Sleep(200);
    cout <<"                      s\n";
    Sleep(200);
    cout <<"                       i\n";
    Sleep(200);
    cout <<"                        n\n";
    Sleep(200);
    cout <<"                         g\n";
    Sleep(200);
    cout <<"                          \n";
    Sleep(200);
    cout <<"                           G\n";
    Sleep(200);
    cout <<"                            a\n";
    Sleep(200);
    cout <<"                             m\n";
    Sleep(200);
    cout <<"                              e\n";
    Sleep(200);
    cout <<"                               !\n\n\n\n";
    Sleep (2000);
    system("cls");
    cout<< "This is a fairly hard game, so if you can win,\n";
    cout << "you're either really lucky or extrememly smart.\n\n\n\n";
    system("pause");
    system("cls");
    }
    
    int easy()                                                        
    { 
    int wingame=0;
    int answer;
        srand((unsigned)time(0));
    int random = rand();  
    int trys =15;
     
    cout << "Here we go...\n\n\n\n\n";
    
    
    do 
    { 
         
        cout << "What is your guess? -";
        cin >> answer;
        
    if (answer < random)
    { 
               
               cout << "Too Small!\n\n\n";
               trys = trys - 1;
               cout << "Only " << trys <<" trys left!\n\n";
    }
    
    if (answer > random)
    {
               cout << "Too Big!\n\n\n";
               trys = trys - 1;
               cout << "Only " << trys <<" trys left!\n\n";
    } 
    
    if (answer ==  random)
    {
               cout << "Great!\n\n\n";
               wingame = 1;
               Sleep(200);
               title();          
    }
    }
    while (wingame == 0, trys > 0);
    if (trys == 0)
    {cout <<"Sorry! You're out of trys!";
    }
    Sleep(3000);
    system("cls");
    if (wingame == 1)
    {
    title();
    }
    }
    
    int menu()
    {
        
        int level;
    cout << "^^-----**Menu**-----^^\n\n\n\n";
    
    cout << "1. Begin the Game\n";
    cout << "2. Read Instructions\n\n\n";
    // input level of user
    cout <<"What is your choice? -";
    cin >> level;
    if (level == 1)
    {
              easy();
          
    }
    if (level == 2)
    {
    cout << "This is a guessing game!(duh)\n";
    Sleep (1000);
    cout << "You have 10 trys to guess a random number,\n";
    Sleep (1000);
    cout << "which can be either huge or small.\n";
    Sleep (1000);
    cout << "When you finish, e-mail me to tell me how much you hated it!\n\n\n\n\n\n\n\n\n";
    system ("PAUSE");
    system("cls");
    }
    easy();
    }  
    
    int main()
    {
    
    title();
    menu();
    
       
    system("PAUSE");
    return EXIT_SUCCESS;
    
    }
    Last edited by Echo; 08-14-2005 at 05:17 PM.

  2. #2
    Registered /usr
    Join Date
    Aug 2001
    Location
    Newport, South Wales, UK
    Posts
    1,262
    Going straight for your looping condition:-
    Quote Originally Posted by Echo
    Code:
    while (wingame == 0, trys > 0);
    What do you think the comma does here?
    Perhaps you can check your reference material (like a book) to ensure that you have what you want and that's all.

  3. #3
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Indent!!
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  4. #4
    Programmer
    Join Date
    Aug 2005
    Location
    Ohio
    Posts
    8

    Thanks!

    Thanks, I'll check it out. And I'll try harder to indent.

  5. #5
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    And uh, those Sleep's...that's really ugly. Also, you do not need to optimize, just a hell of a lot of cleanup. Try these simple functions out for size, just to start cleaning.

    Code:
    #include <windows.h>
    #include <iostream>
    #include <string>
    
    void slowType(std::string s)
    {
    	for(int i = 0; i < s.length(); i++)
    	{
    		std::cout << s[i];
    		Sleep(200);
    	}
    	std::cout << "\n";
    }
    void cascade(std::string s, int start = 0)
    {
    	for(int i = 0; i < s.length(); i++)
    	{
    		for(int k = 0; k < start + 1; k++)
    		{
    			std::cout << " ";
    		}
    		std::cout<< s[i] << std::endl;
    		start++;
    	}
    }
    
    
    
    int main()
    {
    	std::string a("Hello");
    	slowType(a);
    	cascade("Roflcopter", a.length());
    	return 0;
    
    }

  6. #6
    Programmer
    Join Date
    Aug 2005
    Location
    Ohio
    Posts
    8

    Unhappy Sorry, no Idea...

    Uhh...I'm sorry, but I can't fins anything that tells me what to do, or any remote articles close to it. That comma was something I added, hoping it would work, and i was REALLY happy when the compiler didn't shoot me down. What did I do wrong?

    Oh, and Tonto, I'm sorry, but I'm really teaching myself, nd I'm not quite as far as your example yet. As soon as I am, I'll try to implement it.
    Last edited by Echo; 08-14-2005 at 06:01 PM.

  7. #7
    Banned
    Join Date
    Jun 2005
    Posts
    594
    putting a ';' after a conditional statment, kind stops it dead,
    you need to get rid of it, and use brackets around
    the code you want effected by your conditional statment.

  8. #8
    Registered /usr
    Join Date
    Aug 2001
    Location
    Newport, South Wales, UK
    Posts
    1,262
    ILV, if you're referring to the "while" bit, there's a "do" towards the top of that block that starts it off, so the semicolon's supposed to be there. Hard to see it when all the brackets appear to be on the same level, but...

    Echo, the comma doesn't denote a conditional (bitwise) AND. Try searching around for what does.

  9. #9
    Programmer
    Join Date
    Aug 2005
    Location
    Ohio
    Posts
    8

    Smile Thanks Again!

    Thank a lot Smurf! I forgot all about boolean operators! And I'll work on cleaning the code.

  10. #10
    Programmer
    Join Date
    Aug 2005
    Location
    Ohio
    Posts
    8

    But....

    I got the boolean operator &&, but the problem still persists. Any idea why?

  11. #11
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,499
    It's normally a good idea to post your latest effort, especially after making a number of changes. We can only guess as to the actual changes you've made.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Proposal: Code colouring
    By Perspective in forum A Brief History of Cprogramming.com
    Replies: 28
    Last Post: 05-14-2007, 07:23 AM
  2. Explain this C code in english
    By soadlink in forum C Programming
    Replies: 16
    Last Post: 08-31-2006, 12:48 AM
  3. Updated sound engine code
    By VirtualAce in forum Game Programming
    Replies: 8
    Last Post: 11-18-2004, 11:38 AM
  4. wanna read some code
    By snakattak in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 06-19-2002, 04:21 PM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21