Thread: Newbie Question

  1. #1
    Registered User CompiledMonkey's Avatar
    Join Date
    Feb 2002
    Location
    Richmond, VA
    Posts
    438

    Newbie Question

    Could anyone point out what I'm doing wrong?

    Code:
    #include <stdafx.h>
    #include <iostream.h>
    #include <stdio.h>
    
    string userName;
    
    string getName() {
    	userName = "Newb";
    	return userName;
    }
    
    void main(int argc, char* argv[]) {
    	string name = getName();
    	printf("Hello " + name\n");
    }

  2. #2
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    printf("Hello " + name\n");

    Look at the quotes in that line.
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  3. #3
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Code:
    #include <string>
    #include <iostream>
    using std::cout;
    using std::endl;
    using std::string;
    
    
    const char* getName() {
    	return "Newb";
    }
    
    int main(int argc, char* argv[]) {
    	string name = getName();
    	cout << name << endl;
    	return 0;
    }

  4. #4
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    Think about where the data is being allocated. If it is allocated on the stack of a function than it can not be returned to the caller. You will have to allocate the memory from the free store.

  5. #5
    Registered User CompiledMonkey's Avatar
    Join Date
    Feb 2002
    Location
    Richmond, VA
    Posts
    438
    Originally posted by SilentStrike
    printf("Hello " + name\n");

    Look at the quotes in that line.
    That wasn't the error I was getting in the compiler but thanks.

    It should be...

    Code:
    printf("Hello " + name + "\n");

  6. #6
    Registered User CompiledMonkey's Avatar
    Join Date
    Feb 2002
    Location
    Richmond, VA
    Posts
    438
    Originally posted by Fordy
    When I try to compile that, I'm getting this error message.

    Code:
    fatal error C1010: unexpected end of file while looking for precompiled header directive
    I didn't think using a string would be so difficult (or more than it should be) in C++.

  7. #7
    Registered User CompiledMonkey's Avatar
    Join Date
    Feb 2002
    Location
    Richmond, VA
    Posts
    438
    Originally posted by Troll_King
    It's all crap, everything is wrong. This is the worst program that I have ever seen. I'm not joking around.
    Considering it's my first C++ program ever, relax. You should obviously be able to tell I have a Java background.

  8. #8
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784

    Re: Newbie Question

    Originally posted by AMDPwred
    Could anyone point out what I'm doing wrong?

    Code:
    #include <stdafx.h>
    #include <iostream.h>
    #include <stdio.h>
    
    string userName;
    
    string getName() {
    	userName = "Newb";
    	return userName;
    }
    
    void main(int argc, char* argv[]) {
    	string name = getName();
    	printf("Hello " + name\n");
    }
    In C++ you use a referece to collect string information from the caller.

    Code:
    #include <iostream>
    #include<string>
    
    using std::string;
    using std::cout;
    using std::endl;
    
    void getName(string&);
    
    int main() {
    	string name;
    	getName(name);
    
    	cout << "Hello " << name << endl;
    	return 0;
    }
    
    void getName(string& name) {
    	name = "Newb";
    }

  9. #9
    Registered User
    Join Date
    Jun 2002
    Posts
    151
    When I try to compile that, I'm getting this error message.


    code:--------------------------------------------------------------------------------
    fatal error C1010: unexpected end of file while looking for precompiled header directive
    --------------------------------------------------------------------------------


    I didn't think using a string would be so difficult (or more than it should be) in C++.
    You need to add the line

    #include <stdafx.h>

    or remove the precompiled header option from your compiler options.


    I didn't think using a string would be so difficult (or more than it should be) in C++.
    ....
    Considering it's my first C++ program ever, relax. You should obviously be able to tell I have a Java background.
    I wouldn't expect to be able to write a simple program in any language without learning the basic syntax. It doesn't matter what background you've got.

    Think about where the data is being allocated. If it is allocated on the stack of a function than it can not be returned to the caller. You will have to allocate the memory from the free store.....

    ..In C++ you use a referece to collect string information from the caller.
    There's nothing wrong with Fordy's example. The only variable declared on the stack was a pointer, which was copied by value to the caller. In C++ (along with numerous other languages) passing a variable by reference is just one option.

  10. #10
    Registered User
    Join Date
    May 2002
    Posts
    317
    If it is allocated on the stack of a function than it can not be returned to the caller. You will have to allocate the memory from the free store.
    Code:
    int blah(){
          int x = 5
          return x;
    }
    
    int main(){
    
        int y = blah();
        cout<<y<<endl;
        return(0);
    }
    If your going to be mean at least be right. You can allocate data on the stack of a function and return its value to the caller which is what the original code did and what this code does. Yes if you returned a pointer this way it would be rather pointless however in returning a value like above the compiler will generate a temp variable to hold the value between the function terminating and then the assignment in the calling block.

  11. #11
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    If I'm not right than I'm left, but either way I'll knock you out cold and you know it. I wouldn't teach someone a method that will land them into trouble. When you collect dynamic information from a caller in the form of an array than define it globally.

  12. #12
    Registered User
    Join Date
    May 2002
    Posts
    317
    Defining global variables when you do not need to will tend to land someone in trouble, not to mention just poor practice. If you are going to collect information for the caller, it is better simply to pass the array by reference to the function as oppose to defining a global variable. Lets keep in mind the goals of OOP.

  13. #13
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    There is nothing wrong with defining a variable with global scope. Another name for it is the heap. I passed a string by reference in the example that I provided above.

    Character arrays are refereced by pointers.

  14. #14
    Registered User
    Join Date
    May 2002
    Posts
    317
    No, there is nothing wrong with defining a variable with global scope, technically that is. However if it does not need to have global scope don't give it that. My objection was to this general statement:
    When you collect dynamic information from a caller in the form of an array than define it globally
    This is not always the best way to go.

  15. #15
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Originally posted by Enmeduranki

    There's nothing wrong with Fordy's example. The only variable declared on the stack was a pointer, which was copied by value to the caller. In C++ (along with numerous other languages) passing a variable by reference is just one option.
    Exactly......

    If anyone doesnt believe it, compile & link the app and put it into a hex editor....

    The compiler saw that string as a constant string literal and added the string to readonly data section of the exe...(.RDATA section on MSVC++)....so there it sits as global const data....my func then passes a 32bit address (pointer) of that data (by value - as pointers are variables too) to the calling function and it passes that to cout to read the string.........nothing nasty there......

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Stupid Newbie question
    By TimL in forum C++ Programming
    Replies: 4
    Last Post: 07-22-2008, 04:43 AM
  2. C prog newbie question
    By Draginzuzu in forum C Programming
    Replies: 1
    Last Post: 02-03-2003, 06:45 PM
  3. a stupid question from a newbie
    By newcomer in forum C++ Programming
    Replies: 4
    Last Post: 01-11-2003, 04:38 PM
  4. confusion with integers (newbie question)
    By imortal in forum C Programming
    Replies: 7
    Last Post: 12-06-2002, 04:09 PM
  5. newbie class templates question
    By daysleeper in forum C++ Programming
    Replies: 2
    Last Post: 09-18-2001, 09:50 AM