C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 



Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old May 8th, 2015, 12:57 PM
MuckingFedic MuckingFedic is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: May 2015
Posts: 2 MuckingFedic User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 27 m 45 sec
Reputation Power: 0
Exclamation General - Error when trying to read random words from a file

Hello World!
I am currently writing a password generator in Microsoft Visual Studios 2010 Professional. The section I am having a problem with is the practical password. It is suppose to randomly read 3 words from a text file and then display it in the text box. The program will compile and run but when I hit generate I get "True True True" and not three random words. Then this warning shows up:

Warning C4800: 'char *' : forcing value to bool 'true' or 'false' (performance warning)

Can anyone help point out where I am going wrong?

Thanks!


//Code is from form1.h file. I can post the rest of the code if need be but I just included my includes and the problematic section

Code:
#pragma once
#include <cstdlib>
#include <ctime>
#include <stdlib.h>
#include <cmath>
#include <iostream>
#include <iomanip>
#include <string>
#include <time.h>
#include <vector>
#include <fstream>

//PROBLEM SECTION START
private: System::Void button2_Click(System::Object^  sender, System::EventArgs^  e) {

		//Initializing the Variables
		double word1, word2, word3;							    
		char wordone[50],wordtwo[50],wordthree[50];
		unsigned seed = time(0);							   //Resets Time for the seed
	

		//Creating the random values
		srand(seed);
		seed = seed + (rand() % 100);
		word1 = (rand() % 34397) + 1;
		seed = seed + (rand() % 100);	   //Seed will randomize the randomize so that we have a random in our random making everything more random
		word2 = (rand() % 34397) + 1;     //This will create 3 random numbers that will then pull 3 differnt words from file
		seed = seed + (rand() % 100);
		word3 = (rand() % 34397) + 1;

		std::ifstream dic1 ( "dictionary1.txt" );
		std::ifstream dic2 ( "dictionary2.txt" );  //Opening up the files      34,397 words in each file      They are the exact same
		std::ifstream dic3 ( "dictionary3.txt" );
		

		//Loops
		for(int i=1; i <= word1; i++)
		{
			dic1 >> wordone;
		}
		
		for(int i=1; i <= word2; i++)
		{	
			dic2 >> wordtwo;
		}
		

		for(int i=1; i <= word3; i++)
		{
			dic3 >> wordthree;	
		}

	    //Where the error is occurring 
		tbprac -> Text = System::Convert::ToString(wordone) + " " + System::Convert::ToString(wordtwo) + " " + System::Convert::ToString(wordthree);


		 
}
//PROBLEM SECTION END



Screenshot of the warnings:
http://imgur.com/rNhR5L4

Picture of the output:
http://imgur.com/LZN2VYi

Reply With Quote
  #2  
Old May 14th, 2015, 01:33 AM
Thinias Thinias is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2010
Posts: 78 Thinias User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 16 h 58 m 48 sec
Reputation Power: 8
This is a great question.

System::Convert::ToString() is a .Net function, see the documentation here: (https://msdn.microsoft.com/en-us/library/system.convert.tostring(v=vs.110).aspx).

This entire library was built more with C# in mind than C++, and as a result it doesn't have complete support for some C++ constructs... like strings represented by char*. You are trying to access an overload for System::Convert::ToString(char* input)... but that overload doesn't exist, so you are being forced into the nearest matching neighbor.

In c++, you can do things like this:
Code:
char* pointer = NULL;
if (pointer)
{
}


This is the same concept as what's happening to you right now - you are passing in a char* (actually a char[], but that's really just a char* on the stack, with its length bound at compile time), but char* isn't a valid data type for an if statement - you need a bool. The result is that the compiler automatically interprets the above code, assuming you meant this instead:

Code:
if (pointer != NULL)
{
}


Because System::Convert::ToString(char* input) isn't a valid overload for this function, the compiler is automatically interpreting the result to match System::Convert::ToString(bool input) instead... giving you generated code that actually looks like this:

Code:
System::Convert::ToString(wordone != NULL)


Obviously, wordone != NULL will always return true in your program... so the result of mapping true to a string yields "True". Now you can see where your output is coming from .


As for how to fix it... I don't know what the data type of tbPrac->Text is, but seeing as you're using std::ifstream, I'll assume it's compatible with std::string. Maybe try something like this instead:

Code:
using std::string;
...
tbPrac->Text = string(wordone) + string(" ") + string(wordtwo) + string(" ") + string(wordthree);


As a footnote, this would probably all be cleaner (and more robust) if you dropped the char* (char[]) objects entirely and used std::string from the beginning:

Code:
using std::string;
...
// use std::string instead of char[50]
string wordone, wordtwo, wordthree;
...
// all of the file reading code stays the same, but now you read from the std::ifstreams directly into std::string objects instead.  this way you don't have to worry about buffer overflow if the word happened to be greater than 49 characters.
...
// because you're already using std::string objects, you don't need to manually convert your char arrays to std::strings during the concatenation.
tbPrac->Text = wordone + " " + wordtwo + " " + wordthree;

Good luck!
-Thinias

Last edited by Thinias : May 14th, 2015 at 02:13 AM.

Reply With Quote
  #3  
Old May 14th, 2015, 10:15 AM
MuckingFedic MuckingFedic is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: May 2015
Posts: 2 MuckingFedic User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 27 m 45 sec
Reputation Power: 0
Thumbs up

Wow. Thank you. I just got an answer to this question on another forum today too but yours is a 100x more in depth. I really appreciate you taking your time writing this out for me. I actually understand what is going on now.

As for the solution, the guy in the other forum had me do something else. I'll post his solution as soon as I get to a desktop.

Thank You Bud So Much.
You're an amazing human ♡

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > General - Error when trying to read random words from a file


Developer Shed Advertisers and Affiliates


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.

© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap