Weird Output when I compile?

This is a discussion on Weird Output when I compile? within the C++ Programming forums, part of the General Programming Boards category; HOw come I am getting some weird output when I go and compile it? Code: int main() { char c, ...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    17

    Weird Output when I compile?

    HOw come I am getting some weird output when I go and compile it?
    Code:
    int main()
    {   
    	char c, ch, ch1, ch2;
    	string word;
    	string word1;
    	//char word[50];
    	//char word1[25];
    	int length, i, next_guess, num_letters, m, x;
    
    	do
    	{  
    		do
    		{
    		cout<<"\n\n\n\t******************************************************************\n";
    		cout<<"\t******************* PrOgRaMmInG VoCaB GuEsSeR ********************\n";
    		cout<<"\t******************************************************************\n\n\n";
    		cout<<"\n\t(S) Start the Game\n\n\t(Q) Quit\n\n\n\t Enter your choice: ";
    		
    		cin>> ch2;
    
    		}while	(ch2 != 's' && ch2 != 'S' && ch2 != 'Q' && ch2 != 'q');
    	
    	if	(ch2 == 'Q' || ch2=='q')  exit (0);
    
    	if	(ch2 == 's' || ch2=='S')
    
    	{
    		ifstream fin("words.txt");
    		if	(!fin)
    		{ 
    		cout<<"\n\nYou are missing a file called words.txt\n\n"; 
    		
    		return 0;
    		}
    		
    		for (i=0;!fin.eof();i++)   
    		getline(fin, word);//fin.getline(word,25);
    		fin.close();
    
    		do 
    		{
    		x=rand();
    		}while(x>i || x<0);
    
    		ifstream finn("words.txt");
    		
    		for (i=0;!finn.eof();i++)
    
    		{finn>>c; getline(finn,word)/*finn.getline(word,25)*/; if (x==i) break;}
    		finn.close();
    	}
    	
    	if	(ch2 == 'S' || ch2=='s')
    	{
    		length = word.length(); /*length=strlen(word);*/			//Computes the length of the string
    		char choosen[50]="\0";
    		num_letters=0; 
    		m=0;
    
    	for	(i=0; i<=24; i++)
    	
    	{
    		if (word[i]=='\0') {word1[i]='\0'; break;}
    		if (word[i]==' ')  {word1[i]=' ';  num_letters++;}
    		if (word[i]!=' ')  word1[i]='-';
    	}
    
    	next_guess=length+2-num_letters;     //Number of Guesses Left
    	do
    	{
    		definition:  define_word(c);
    		if (m!=0)  cout<<"\n\n\t\t\tChoosen letters : "<<choosen<<"\n";
    		cout<<"\n\n\n\t\t\t      "<<word1;
    		cout<<"\n\n\n\tYou have "<<next_guess<< " guesses left, choose a letter : ";
    		cin>>ch; cin.get();
    		for (i=0;i<25;i++) 
    			
    		if (choosen[i]==ch) 
    		{
    		cout<<"You have choosen "<< ch <<" already!!\n"; goto definition;
    		}
    		
    		next_guess--; 
    		choosen [m]=ch; 
    		choosen [m+1]=',';
    		m+=2;
    
    		for (i=0;i<=49;i++)    
    			
    		if (word[i]==ch || word[i]==ch+32 || word[i]==ch-32) word1[i]=ch;
    		if (word==word1)//(!strcmpi (word1,word)) 
    		{
    			cout<<"\n\t\t\t      "<<word;//strupr(word);
    			cout<<"\n\n\t\t\tCongratulations, You have won!\n"; 
    			break;
    		}
    
    	}while(next_guess>0 || word==word1/*!strcmpi (word1,word)*/);
    
    		if (word1==word)/*(strcmpi (word1,word)) */ 
    		
    		cout<<"\n\tBETTER LUCK NEXT TIME.\n\n\tTHE WORD IS : "<</*strupr*//*(*/word/*)*/<<endl;
    	}
    	
    	cout<<"\nWould you like to play again? [Y or N] : ";
    	cin>>ch1;  cin.get();
    
    	}while (ch1=='y' || ch1=='Y');
    
    		system("PAUSE");
    		return 0;
    }

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,340
    What's the weird output?

    BTW, word1[i]='\0' is wrong. The string type starts empty. You cannot access any characters of an empty string. This might lead to a crash, or weird output or just an incorrect answer some of the time. If you want to add a space to the string, use +=. Also, the C++ string class doesn't have a null terminator. It doesn't need one because it keeps track of its own length. You'll have to rethink some of the code you are using so that it works with the C++ string class.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Maybe you need some header files, like <iostream> and <string>.

    [edit]
    And <cstdlib>, for system().
    [/edit]

    BTW, string==string isn't the same as stricmp(), it's the same as strcmp().
    Code:
    if (word1==word)/*(strcmpi (word1,word)) */
    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.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,671
    > HOw come I am getting some weird output when I go and compile it?
    Dunno - you only posted the weird input to the compiler, not the wierd output.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help for my output array
    By qwertysingh in forum C Programming
    Replies: 1
    Last Post: 02-17-2009, 02:08 PM
  2. Compile as you type
    By Rocketmagnet in forum A Brief History of Cprogramming.com
    Replies: 33
    Last Post: 12-07-2006, 12:36 PM
  3. Dev-C++: Can anyone please compile this?
    By SG57 in forum C Programming
    Replies: 6
    Last Post: 08-31-2006, 08:30 AM
  4. Base converter libary
    By cdonlan in forum C++ Programming
    Replies: 22
    Last Post: 05-15-2005, 01:11 AM
  5. Weird (!) gcc (g++) compile error
    By Skarr in forum C++ Programming
    Replies: 3
    Last Post: 08-21-2002, 05:09 PM

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