pass string into main

This is a discussion on pass string into main within the C++ Programming forums, part of the General Programming Boards category; hi i need to pass a string into a program of mine but this doesn't seem to be working. its ...

  1. #1
    Wanabe Laser Engineer chico1st's Avatar
    Join Date
    Jul 2007
    Posts
    168

    pass string into main

    hi
    i need to pass a string into a program of mine but this doesn't seem to be working.

    its been a while but this isnt working and im frustrated

    Code:
    #include "stdio.h"
    #include "string.h"
    
    void main(char badsmell[30]){
    	printf("%s", badsmell);
    	return;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,854
    Read the FAQ (or any good book) to find out about argc and argv.
    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.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    also
    Code:
    #include <cstdio>
    #include <cstring>
    Last edited by robwhit; 07-21-2007 at 06:41 PM. Reason: thought I was in the C board

  4. #4
    Registered User
    Join Date
    Jul 2007
    Posts
    36
    Code:
    #include <string.h>
    
    int main(char** argv, int argc)
    {
    
        char badsmell[30];
    
        if (argc == 1)
        {
    
            strcpy(badsmell, argv[1]);
    
        }
    
    }

  5. #5
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Code:
    #include <string.h>
    That's <cstring> in C++.

    Code:
    int main(char** argv, int argc)
    That's the other way round.
    Code:
        char badsmell[30];
    How do you know this is long enough?

    Code:
        if (argc == 1)
        {
    
            strcpy(badsmell, argv[1]);
    
        }
    This will always go out of bounds.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    Seriously, watch Salem's avatar until you understand the message it contains.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  7. #7
    Registered User Frobozz's Avatar
    Join Date
    Dec 2002
    Posts
    546
    Code:
    #include <cstdio>
    #include <cstdlib>
    #include <cstring>
    
    int main(int argc, char **argv)
    {
    	char *badsmell = NULL;
    	
    	if (argc == 2)
    	{
    		int size = strlen(argv[1]);
    		badsmell = (char *)malloc(sizeof(char) * size);
    		strcpy(badsmell, argv[1]);
    		
    		printf("&#37;s\n", badsmell);
    	}
    	
    	if (badsmell) free(badsmell);
    	return 0;
    }
    Of course this being C++, you could also do

    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main(int argc, char **argv)
    {
    	string badsmell;
    	
    	if (argc == 2)
    	{
    		badsmell = argv[1];
    		
    		cout << badsmell << endl;
    	}
    	
    	return 0;
    }
    Last edited by Frobozz; 07-21-2007 at 11:16 PM.

  8. #8
    Registered User
    Join Date
    Jul 2007
    Posts
    36
    Quote Originally Posted by iMalc View Post
    Seriously, watch Salem's avatar until you understand the message it contains.
    That's a good point. Never use void main().
    FrancoisSoft
    FrancoisSoft on the Web
    If you stare at a computer for 5 minutes you might be a nerdneck.

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, 04:13 PM
  2. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 11:06 PM
  3. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 02:45 PM
  4. string handling
    By lessrain in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 08:36 PM
  5. can c let me pass string references??
    By adaly in forum C Programming
    Replies: 9
    Last Post: 01-09-2002, 04:17 PM

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