Thread: std::cout not working

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    128

    [RESOLVED]std::cout not working - function call issue

    I'm new to C++ but i've been programming in other languages for years...

    I've slowly going through the tutorials here and building a very basic application based on what I learn each time. At this point, the user runs the program from a command prompt and passes the name of an existing txt file like (myprogram.exe lab1.txt). Then, all user interaction is logged to that txt file. So far, all of the code works.

    I'm trying to add a linked list to store a list of Lab names and then print then out. My next step is to validate against it - but i'm not quite there.

    With the code listed below, the program runs but shows no sign that the StoreLabList process has run. I've noticed that it's the only time I've had to use using namespace std and also tried to use cout. I've searched and found a fix of "using std:cout;" but that's not doing it. One of the first pieces of this function is spit out "cout<< "Lab Names\n";" but it's not happening and neither are my lab names being printed. yet the program appears to run through the code. I think it has to do with the namespaces but as I'm new to this...help.

    the code:
    Code:
    #include <iostream.h>
    #include <stdlib.h>
    #include <string.h>
    #include <fstream>
    //------------------------------------------------------------
    struct workdetails {
    	char labname[12];
    	int accesslevel;
    	int toxincheck;
    	int prioritylevel;
    };
    
    struct node {
      char labname[10];
      node *next;
    };
    //------------------------------------------------------------
    int StoreLabList();
    int GetToxicityLevel();
    int GetAuthorityLevel();
    int GetPriorityLevel();
    int PrintAccess(workdetails, char currentfilename[20]);
    int ValidateGoodFile(char filename[20]);
    int PrintLabList();
    
    
    
    //Scientist class
    class Scientist
    {
    private:	
    	int toxin;
    public:
    	//constructor with initialization list
    	Scientist(int Toxin_Value) : toxin(Toxin_Value)
    	{}
    
    	//member functions which do something with toxin
    	int GetToxin() //return the value of toxin
    	{
    		return toxin;
    	}
    	void AlterToxin(int Toxin_Value) //set toxin to Toxin_Value
    	{
    		toxin = Toxin_Value;
    	}
    };
    
    class QA
    {
    public:
    	void Good_RestoreToxin(Scientist& worker, int value)
    	{
    		if (worker.GetToxin() != value)
    		worker.AlterToxin(value); //if toxic level different, change back.
    	}
    };
    
    //Cleaner class
    class Cleaner
    {
    public:
    	void Evil_Altertoxin(Scientist& victim, int value)
    	{
    		victim.AlterToxin(value); 
    	}
    };
    
    //void main()
    int main ( int argc, char *argv[] )
    {
    
    	char InputLNAME[11]; //LabName
    	int IsotopeVar = 0; //will store correct toxin value
    	int thisisanumber; //input variable
    	int validtoxin = 0; // false
    	char currentfilename[20]; //log file name dependent on parm
    	int validarguments = 0;
    
    	if ( argc != 2 ) // argc should be 2 for correct execution
    		// We print argv[0] assuming it is the program name
    		{
    		cout<<"usage: "<< argv[0] <<" <filename>\n";
    		validarguments = 0;
    		}
    	else {
    		// We assume argv[1] is a filename to open
    		if ( ValidateGoodFile(argv[1]) == 0 ) { 
    			strcpy (currentfilename, "default.txt"); }
    		else
    			{
    			strcpy (currentfilename, argv[1]);
    			}
    		validarguments = 1;
    		}
    
    	if ( validarguments == 0 ) { exit(0); }
    	
    	if ( StoreLabList == 0 ) { exit(0); }
    
    	//assign to struct
    	workdetails LabAccess;
    	
    	cout << "Please enter a lab name: ";
    	cin.getline ( InputLNAME, 11, '\n' );              
    	strcpy (LabAccess.labname, InputLNAME);
    
    	LabAccess.accesslevel = GetAuthorityLevel();
    	if ( LabAccess.accesslevel == 0 ) { exit(0); }
    
    	LabAccess.prioritylevel = GetPriorityLevel();
    
    	// loop until valid toxicity level
    	do {
    		thisisanumber = GetToxicityLevel();
    		if ( thisisanumber < 100 ) {                  
    			cout<<"Toxic level within range\n"; // Just to show you it works...
    			validtoxin = 1;
    			}
    		else if ( thisisanumber == 100 ) {            // I use else just to show an example 
    			cout<<"Extreme toxic level. \n";           // Just to show you it works...
    			if (LabAccess.prioritylevel == 5) {
    				validtoxin = 1;
    				}
    			else {
    				cout<<"You do not have an appropriate priority level.\n";
    				validtoxin = 0;
    				}
    			}
    		else {
    			cout<<"Toxic level incorrect\n";     // Executed if no other statement is
    			}
    	} while (validtoxin == 0);
    	LabAccess.toxincheck = thisisanumber;
    	IsotopeVar = thisisanumber;
    
    	if (PrintAccess(LabAccess,currentfilename) == 0) {
    		cout <<"Error writing to log.\n";}
    
    	//create object Dave - instance encapsulation
    	Scientist Dave(IsotopeVar); 
    
    	//create object John
    	Cleaner John; 
    
    	//Dave checks to see if toxin is still unaltered
    	cout << "\nScientist Dave:\n";
    	if(Dave.GetToxin() == IsotopeVar)
    		cout << "I'm glad nobody messed with my toxin!\n";
    	else
    		cout << "Oh my god, somebody altered my toxin!\n";
    
    	//John attempts to alter toxin
    	cout << "\nEVIL Cleaner John:\nLet's try to alter Dave's toxin!\n";
    	John.Evil_Altertoxin(Dave, 0);
    	
    	cout << "\nScientist Dave:\n";
    	if(Dave.GetToxin() == IsotopeVar)
    		cout << "I'm glad nobody messed with my toxin!\n";
    	else
    	{
    		cout << "Oh my god, somebody altered my toxin!\n";
    		cout << "\nUseful Intern:";
    		cout << "\nI'll fix it.\n";
    		QA Intern;
    		Intern.Good_RestoreToxin(Dave,IsotopeVar);
    		cout << "\nScientist Dave:\n";
    		if(Dave.GetToxin() == IsotopeVar)
    			cout << "I'm glad someone fixed my toxin!\n";
    		else
    			cout << "Oh my god, my toxin can't be fixed!\n";
    	}
    	return 1;
    
    	//getche(); //just so the program doesnt terminate immediatly
    }
    
    int PrintAccess(workdetails Details, char currentfilename[20])
    {
    	using namespace std;
    	ofstream a_file ( currentfilename,ios::app );
    	//ios::app   -- Append to the file
    	//ios::ate   -- Set the current position to the end
    	//ios::trunc -- Delete everything in the file
    
    	//test file
    	if ( !a_file.is_open() ) {
    		return 0;
    	}
    	else {
    		// Safely use the file stream
    		// Outputs to example.txt through a_file
    		a_file << Details.labname<<"-"<<Details.accesslevel<<"-"<<Details.toxincheck<<"-
    "<<Details.prioritylevel<<"\n";
    		// Close the file stream explicitly
    		a_file.close();
    		return 1;
    	}
    
    }
    
    int ValidateGoodFile(char filename[20])
    {
    	using namespace std;
    	ifstream the_file ( filename );
    	// Always check to see if file opening succeeded
    	if ( !the_file.is_open() )
    		return 0;
    	else 
    		return 1;
    	// the_file is closed implicitly here
    	the_file.close();
    }
    
    int GetToxicityLevel()
    {
    	int GTLevel;
    	cout<<"Please enter a toxicity level: ";
    	cin>> GTLevel;
    	cin.ignore();
    	return GTLevel;
    }
    
    int GetPriorityLevel()
    {
    	int GPLevel;
    	cout<<"Please enter a priority level: ";
    	cin>> GPLevel;
    	cin.ignore();
    	return GPLevel;
    }
    
    int GetAuthorityLevel()
    {
    	int GALevel;
    	cout<<"Please enter your access level: ";
    	cin>> GALevel;
    	cin.ignore();
    
    	switch ( GALevel ) {
    	case 1:
    		return 1;
    		break;
    	case 2:
    		return 1;
    		break;
    	case 3:
    		return 1;
    		break;
    	default:
    		return 0;
    		break;
    	}
    }
    
    
    int StoreLabList()
    {
    
    	using namespace std;
    	cout<< "Lab Names\n";
    	node *root;       // This won't change, or we would lose the list in memory
    	node *conductor;  // This will point to each node as it traverses the list
    	ifstream the_file ( "lablist.txt" );
        // Always check to see if file opening succeeded
        if ( !the_file.is_open() ){
    		cout <<"Could not open file\n";
    		return 0;
    		}
        else {
    		root = new node;  // Sets it to actually point to something
    		root->next = 0;   //  Otherwise it would not work well
    		strcpy(root->labname,"Other");
    		conductor = root;
    		char labinput[10];
    		// the_file.get ( labinput ) returns false if the end of the file
    		//  is reached or an error occurs
    		while ( the_file.get ( labinput, 11, '\n' ) ){
    			conductor->next = new node;  // Creates a node at the end of the list
    			conductor = conductor->next; // Points to that node
    			conductor->next = 0;         // Prevents it from going any further
    			strcpy(conductor->labname, labinput);
    			}
    		the_file.close();
    		conductor = root;
    		while ( conductor != NULL ) 
    			{
    			cout<< conductor->labname;
    			conductor = conductor->next;
    			}
    		return 1;
    		}
    }
    Last edited by FoodDude; 08-12-2005 at 02:50 PM.

  2. #2
    I am me, who else?
    Join Date
    Oct 2002
    Posts
    250
    First remove #include <iostream.h>
    Second move the using namespace std; to just under your includes.
    I will be trying this code out here so before I say any more I will try it out.

    Additionally if you want to stop the program for exiting immediately, you can do this.

    at the end of main(before return) type this:

    Code:
    char c;
    cin >> c;
    then just type a char and hit enter.
    Last edited by dpro; 08-12-2005 at 02:12 PM. Reason: Adding more

  3. #3
    Registered User
    Join Date
    Aug 2005
    Posts
    128
    Oh, MS VC++ 6.0

    I tried your changes but still no results, yet it compiles file.

  4. #4
    I am me, who else?
    Join Date
    Oct 2002
    Posts
    250
    On my vs.net 2003 it seems to work fine. Incidentally vc6 is not really the best for standards compliant (at least the latest version of C-standards (c99 I believe)

  5. #5
    Registered User
    Join Date
    Aug 2005
    Posts
    128
    I've got vb.net but I haven't gone c+.net, yet. Since i've got c++ 6.0, it's a great (free) place to start. I'm trying different variations with the namespace - it seems the only thing that makes sense. I just ran the program and it still works except for that one function. Thanks for your help, BTW, dpro.

  6. #6
    Wen Resu
    Join Date
    May 2003
    Posts
    219
    #include <iostream.h>
    change that to
    #include <iostream>

    using std::cout;
    using std::cin;
    or
    using namespace std


    reasoning..
    the .h headers are c style ones. one I showed is a newer C++ lib.

  7. #7
    Registered User
    Join Date
    Aug 2005
    Posts
    128
    This is why I'm not a detective - I look in the wrong places...


    I had :
    Code:
    if ( StoreLabList == 0 ) { exit(0); }
    I should have had:
    Code:
    if ( StoreLabList() == 0 ) { exit(0); }
    StoreLabList = variable
    StoreLabList() = Function

    Thanks for your help to everyone, at least I learned a bit about includes and .h files.

  8. #8
    I am me, who else?
    Join Date
    Oct 2002
    Posts
    250
    Ahh debugging gotta love it . Well I am glad you got it working. helps to breakup the code at times, use h files for class definitions

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    The answers here are confusing to me, so this is how you should have your headers on VC++ 6:
    Code:
    #include <iostream>
    #include <fstream>
    #include <cstdlib>
    #include <cstring>
    
    using namespace std;
    All other references to std are unnecessary if you do it this way, and your code will still work when you move on to a newer compiler.

    You should consider learning the C++ string class, it is easier to use than C style character arrays, and doesn't put restrictions on the size of your string like the array does (what if the lab name is more than 11 characters long??). To use the string class, you need to #include <string>, which is very different from <cstring> or <string.h>. If you decide to learn C++ strings just consult your book or tutorial for more information on them.

  10. #10
    Registered User
    Join Date
    Nov 2002
    Posts
    491
    MSVC++6.0 was created before C++ was even standardized. Since time moves in one directoin this means MSVC++6.0 cannot be compliant wit the C++ standard. SP6 for VC6 does make it slightly more compliant but not really any better. Get gcc or the latest verions of vc++. gcc is free adn so is the compiler the latest version of vc++ uses.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function not working
    By sloopy in forum C Programming
    Replies: 31
    Last Post: 11-12-2005, 08:08 PM
  2. Program Not working Right
    By raven420smoke in forum C++ Programming
    Replies: 2
    Last Post: 09-16-2005, 03:21 AM
  3. Trying to eject D drive using code, but not working... :(
    By snowfrog in forum C++ Programming
    Replies: 3
    Last Post: 05-07-2005, 07:47 PM
  4. x on upper right corner not working
    By caduardo21 in forum Windows Programming
    Replies: 1
    Last Post: 02-20-2005, 08:35 PM
  5. cygwin -> unix , my code not working properly ;(
    By CyC|OpS in forum C Programming
    Replies: 4
    Last Post: 05-18-2002, 04:08 AM