Thread: std::cout not working

Threaded View

Previous Post Previous Post   Next Post Next Post
  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.

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