Thread: Problem !!!

  1. #1
    Registered User
    Join Date
    Jun 2005
    Location
    Sarajevo, Bosnia and Herzegovina
    Posts
    18

    Problem !!!

    Code:
    #include <cstdlib>
    #include <ctime>
    #include <iostream>
    
    using namespace std;
    
    
    void klubovi ( char club1 [100], char club2 [100])
    {
    			cout << "\nUnesite ime domaceg tima: ";
    			cin.getline (club1,100);
    			cin.ignore (100,'\n');
    			cout << "Unesite ime gostujuceg tima: ";
    			cin.get(club2,100);
    };
    	
    int main()
    {
    
    	int novac = 100;
    	while(novac > 0)
    	{
    	novac = 100;
    	char club1 [100];
    	char club2 [100];
    	int broj;
    	cout << "\n\nODABERITE OBLAST: ";
    	cin >> broj;
    	
    		switch (broj)
    		case 1:
    		{
    		    klubovi (club1,club2);
    			cout << club1 << club2;
    srand(time(0));
    			float kv1;
    			kv1 = 1.05 + (rand() % 3);
    			float kv2;
    			kv2 = 1.05 + (rand() % 3);
    			float kv3;
    			kv3 = 2.1 + (rand() % 3);
    		break;
    		case 2:
    		cout << club1 << " : " << club2;	
    		break;
    		case 3:
    			
    			cout << "\n##### KVOTE #####\n";
    			cout <<   "-----------------\n";
    			cout << club1 << ": " << kv1;
    			cout << "\n" << club2 << ": " << kv2;
    			cout << "\nnerijeseno: " << kv3;
    			
    		
    		
    		case 6:
    		srand(time(0));
    	int rez1;
    			rez1 = 0 + (rand()%5);
    
    	int rez2;
    			rez2 = 0 + (rand() % 5);
    			break;
    		}
    }
    	
    }
    When I called function klubovi everithing is ok I put strings club1 and club2, but when I called switch (2) or (3) or just want to cout << club1 << club2; he write me just club2. WHY !!!
    Same thing is if I put function klubovi to switch (1) or enywhere in while ();
    WHY he not erite me club1 ???

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    cout << "\n##### KVOTE #####\n";
    			cout <<   "-----------------\n";
    			cout << club1 << ": " << kv1;
    			cout << "\n" << club2 << ": " << kv2;
    			cout << "\nnerijeseno: " << kv3;
    This shouldn't even compile. There are no variables "kv1", "kv2", and "kv3" at this point in the switch. They are only declared in case 1:. They exit scope at the break statement at the end if case 1, and are no longer declared. Move them out of the switch.

    Now boys and girls, you know why I find the random smattering variable declaration method that C++ has so horrible. Ugly ugly code.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Code:
    cin.ignore (100,'\n');
    That line ignores the newline left in the input stream. It should be put after cin >> broj; and removed from where it is now. It is used after cin >> calls because cin >> leaves the newline from when you press Enter.

    So remove that line from the klubovi function and add it under the cin >> call like this:
    Code:
    cin >> broj;
    cin.ignore (100,'\n');

  4. #4
    Registered User
    Join Date
    Jun 2005
    Location
    Sarajevo, Bosnia and Herzegovina
    Posts
    18
    Quote Originally Posted by Daved
    Code:
    cin.ignore (100,'\n');
    That line ignores the newline left in the input stream. It should be put after cin >> broj; and removed from where it is now. It is used after cin >> calls because cin >> leaves the newline from when you press Enter.

    So remove that line from the klubovi function and add it under the cin >> call like this:
    Code:
    cin >> broj;
    cin.ignore (100,'\n');
    Thank you man, that's work !!!
    I have one more question, which code do this:
    I want to write 10 words or more/less (not matter), and I want to one of that 10 words on random way be displayed.
    Like:
    char word [100];
    cout << word;
    Where is 'word', random choosen word of that 10 words.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    I would use the C++ string class, but you can do it with C style strings also. You need an array (or vector) of strings. Then generate a random number with rand() between 0 and one less than the size of the array. That random number is the index of the randomly chosen word.

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Like this?

    Code:
    char word[10][100] = {
        "hippo",
        "jaguar",
        "rhino",
        "elephant", 
        // ...
    };
    
    int main() {
        cout << word[rand()%10];
        return 0;
    }
    I think that will work, although I haven't tested it.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    Registered User
    Join Date
    Jun 2005
    Location
    Sarajevo, Bosnia and Herzegovina
    Posts
    18
    Quote Originally Posted by dwks
    Like this?

    Code:
    char word[10][100] = {
        "hippo",
        "jaguar",
        "rhino",
        "elephant", 
        // ...
    };
    
    int main() {
        cout << word[rand()%10];
        return 0;
    }
    I think that will work, although I haven't tested it.
    Code:
    #include <iostream>
    #include <ctime>
    #include <cstdlib>
    using namespace std;
    
    int main()
    {
       char word[4][100] = {
        "hippo",
        "jaguar",
        "rhino",
    	"elephant"} ;
    
    
    	srand(time(0));
        cout << word[rand()%4];
        return 0;
    };
    Yeah it work with little changes, I added #include <ctime> and srand(time(0));
    Without this he always choose same word, but with this everything ok.
    Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM