Cout prints address instead of string?

This is a discussion on Cout prints address instead of string? within the C++ Programming forums, part of the General Programming Boards category; The following code prints the address instead of a string in VC ++ 2010 I'm new do I have to ...

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    11

    Cout prints address instead of string?

    The following code prints the address instead of a string in VC ++ 2010 I'm new do I have to do a conversion, is this a bug with VC++?

    Code:
    cout<<"usage: "<< argv[0] <<" <filename>\n";
    I copied it from this example here:

    Cprogramming.com Tutorial: Command-Line Arguments

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Ouch. How about when you run the exe outside of VC? Does this code do the same thing?
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main(int argc, const char *argv[]) {
    	cout << "--> " << argv[0] << endl;
    	return 0;
    }
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,546
    Maybe you mis-declared argv?

    Post your actual code, post your actual output, post your compiler log.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    11
    Here's the code, I made it as simple as possible, just run the exe and get the error, and it prints the address, I also swapped cerr with cout and still got the same error.

    Code:
    int _tmain(int argc, _TCHAR * argv[])
    {
    	if (argc == 1)
    	{
    		cerr << "Usage : " << argv[0] << " filename[s]\n";
    	exit(1);
    	}
    }

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Well, this part is very non-standard:

    Code:
    int _tmain(int argc, _TCHAR * argv[])
    Did you try the program I posted in post #2?
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    11
    Quote Originally Posted by MK27 View Post
    Well, this part is very non-standard:

    Code:
    int _tmain(int argc, _TCHAR * argv[])
    Did you try the program I posted in post #2?
    Your code worked!

  7. #7
    Registered User
    Join Date
    May 2010
    Posts
    11
    Oops new problem, the code now crashes at the end of the program, the program finishes and then I get an error in the visual C dll.

  8. #8
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by sting777 View Post
    Oops new problem, the code now crashes at the end of the program, the program finishes and then I get an error in the visual C dll.
    Post the entire program (or at least the smallest program that causes the problem) in here. Or, since it's barely related to this post, do so in a new thread. You probably have a buffer overflow or something similar.

  9. #9
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,546
    So in other words, _TCHAR is declared as a wide character on your UNICODE enabled build.

    Read your compiler documentation to see how you can cout a TCHAR (regardless of UNICODE mode).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  10. #10
    Registered User
    Join Date
    May 2010
    Posts
    11
    Quote Originally Posted by Salem View Post
    So in other words, _TCHAR is declared as a wide character on your UNICODE enabled build.

    Read your compiler documentation to see how you can cout a TCHAR (regardless of UNICODE mode).
    I still have address issues with some parts of the code, note that this code is just me tooling around with reading files.

    The main function is in rput_readfile, the line that prints addresses is:

    Code:
    cout << ar[0] << "   " <<  ar[1] <<  "     " << ar[2] << "   " << ar[3] << " \n";
    When switching from cout to wcout I get a crash when the program finishes.

    Code:
    void rput_readfile(int a, _TCHAR * ar[])
    {
         int elapTicks;
         double elapMilli, elapSeconds, elapMinutes;
         clock_t Begin, End;             //initialize Begin and End for the timer
    
    	 /*
         Begin = clock() * CLK_TCK;      //start the timer  
         for(int a=1; a<=10000; a++);
         End = clock() * CLK_TCK;        //stop the timer 
         */   
         
        
    
    	ifstream fin;
    	ofstream fout;
    	
    	long count;
    	long total = 0;
    	char ch;
    
    	int spaces=0;
    	int newlines=0;
    	for (int file = 1; file < a; file++)
    
    	{
    		Begin = clock() * CLK_TCK;
    		fin.open(ar[file]);
    		
    		count = 0;
    		fout.open("stupid");
    		while (fin.get(ch))
    		{
    			while(!fout.put(ch));
    			
    			if (ch ==' ')
    				spaces++;
    			else if (ch == '\n')
    				newlines++;
    			count++;
    		}
    		cout << count << " characters in \n" ;
    		wcout << "Argument 1 : " << ar[1] << " \n";
    		total +=count;
    		fin.clear();
    		fin.close();
    		fout.close();
    	
    	}
    	End = clock() * CLK_TCK; 
    	cout << ar[0] << "   " <<  ar[1] <<  "     " << ar[2] << "   " << ar[3] << " \n";
    	cout << total << " characters in all files\n";
    
    
    
    
    
    	 elapTicks = End - Begin;        //the number of ticks from Begin to End
         elapMilli = elapTicks/1000;     //milliseconds from Begin to End
         elapSeconds = elapMilli/1000;   //seconds from Begin to End
         elapMinutes = elapSeconds/60;   //minutes from Begin to End
         
         
         if(elapSeconds < 1)
              cout<<"\n\nIt took "<<elapMilli<<" milliseconds.";
         else if(elapSeconds == 1)
              cout<<"\n\nIt took  1 second.";
         else if(elapSeconds > 1 && elapSeconds < 60)
              cout<<"\n\nIt took  "<<elapSeconds<<" seconds.";
         else if(elapSeconds >= 60)     
              cout<<"\n\nIt took  "<<elapMinutes<<" minutes.";
    
    
    
    
    	 double meg_sec = 0;
    	 double total1 = 0;
    	 total1 = total / 1000000;
    	 meg_sec = total1 / elapSeconds;
    	 cout <<"\nMegs per second was " << meg_sec;
    
    	 printf ("\nDecimals: %d  %ld\n", meg_sec, total);
    	 cout << "\nSpaces were: " << spaces << " Newlines were: " << newlines;
    	 
    	 cout << "\n";
    }
    
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    	if (argc == 1)
    	{
    		wcout << "Usage : " << argv[0] << " filename[s]\n";
    	exit(1);
    	}
    
    	rput_readfile(argc, argv);
    
    	 return 0;
    }
    Last edited by sting777; 06-01-2010 at 10:21 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 03:13 PM
  2. RicBot
    By John_ in forum C++ Programming
    Replies: 8
    Last Post: 06-13-2006, 06:52 PM
  3. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 09:19 AM
  4. Another overloading "<<" problem
    By alphaoide in forum C++ Programming
    Replies: 18
    Last Post: 09-30-2003, 10:32 AM
  5. c++ string input
    By R.Stiltskin in forum C++ Programming
    Replies: 4
    Last Post: 02-22-2003, 03:25 PM

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