Help a new programmer

This is a discussion on Help a new programmer within the C++ Programming forums, part of the General Programming Boards category; I'm new to programming, but with every program I do, I end up with one error. For some reason, I ...

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    5

    Help a new programmer

    I'm new to programming, but with every program I do, I end up with one error. For some reason, I can't figure out how to install MSDN, so I can't press F1 for it to tell me what to fix. Here's my current code:

    Code:
    #include <iostream.h>
    
    char WhatToDo()
    {
    	cout << "What do you want to do?\n\n";
    	return 0;
    }
    
    char OpenGate()
    {
    	cout << "You open the gate and step inside the yard. You marvel at the fact that even\n";
    	cout << "though this yard is tiny, there has apparently been no effort expended toward\n";
    	cout << "it's upkeep. You hear what could be muffled growling coming from your left.\n\n";
    
    	WhatToDo();
    	return 0;
    }
    
    int main()
    {	
    	char a;
    	cout << "You are standing in front of a run-down house. There is a blue sportscar that\n";
    	cout << "looks like it hasn't been driven in years parked out front. The car is covered\n";
    	cout << "in leaves. The house itself looks like it could collapse at any moment.\n";
    	cout << "There is a chain-link fence surrounding the house. The house has a screened-in\n";
    	cout << "porch, but you can't see much beyond that. Planted in the yard are trees which,\n";
    	cout << "if properly pruned, were probably nice bushes at one time. There is a closed\n";
    	cout << "gate which leads through the fence.\n\n";
    	
    		WhatToDo();
    		cin >> a;
    		if (a = "open gate")
    			OpenGate();
    	else cout << "Please enter a valid command.\n";
    
    	return 0;
    
    }
    Anyone see any obvious discrepancies?

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    You didn't post the error.

    It appears you are trying to read a string into a char variable. A char holds only a single character, but a string is a sequence of characters. You should change that to string instead of char.

    To use the string class, you need to #include <string>. While you're at it, you should use <iostream> instead of <iostream.h>, since the latter is outdated, non-standard, and doesn't work on some modern compilers. Finally, by changing to the correct header and using the string class you must sepcify the std namespace. The simplest way is to add "using namespace std;" without the quotes just below the #includes.

    Also, you should note the difference between assignment (with "=") and equality (with "==").

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    5
    I'm not getting an error message because I can't seem to install MSDN, which is supposed to give me the error messages, from what I've read. I've updated my code to what you suggested, or what I could make it out to be, but I still get one error:

    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
     string WhatToDo()
    {
    	cout << "What do you want to do?\n\n";
    	return 0;
    }
    
     string OpenGate()
    {
    	cout << "You open the gate and step inside the yard. You marvel at the fact that even\n";
    	cout << "though this yard is tiny, there has apparently been no effort expended toward\n";
    	cout << "it's upkeep. You hear what could be muffled growling coming from your left.\n\n";
    
    	WhatToDo();
    	return 0;
    }
    
     string main()
    {	
    	char a;
    	cout << "You are standing in front of a run-down house. There is a blue sportscar that\n";
    	cout << "looks like it hasn't been driven in years parked out front. The car is covered\n";
    	cout << "in leaves. The house itself looks like it could collapse at any moment.\n";
    	cout << "There is a chain-link fence surrounding the house. The house has a screened-in\n";
    	cout << "porch, but you can't see much beyond that. Planted in the yard are trees which,\n";
    	cout << "if properly pruned, were probably nice bushes at one time. There is a closed\n";
    	cout << "gate which leads through the fence.\n\n";
    	
    		WhatToDo();
    		cin >> a;
    		if (a = "open gate")
    			OpenGate();
    	else cout << "Please enter a valid command.\n";
    
    	return 0;
    
    }
    Like I said, I'm new. I've been doing this for maybe two days. I'm taking baby steps here.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    >> I still get one error.

    What error? MSDN is just the help file, it shouldn't have anything to do with the error number or message.

    The variable a is declared as a char, but it should be a string since you are typing in a string. You haven't changed that yet.

    Your functions don't return anything useful. They should specify void as the return value instead of string and you should remove the return 0 from them (except for the main() function, which should always specify int like you had originally).

    That should fix your compiler errors. Your program has other issues, like the assignment/equality distinction that I added to my last post. There is also a problem with reading in strings that have a space in them. Until you get the rest of the code working, I'd change "open gate" to "open". Finally, you will want to add a loop in there eventually, but don't worry about that until you get the code to compile and run.

  5. #5
    Bioport Productions
    Join Date
    Oct 2005
    Posts
    215
    Quote Originally Posted by Tarthus
    I'm not getting an error message because I can't seem to install MSDN, which is supposed to give me the error messages, from what I've read. I've updated my code to what you suggested, or what I could make it out to be, but I still get one error:

    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
     string WhatToDo()
    {
    	cout << "What do you want to do?\n\n";
    	return 0;
    }
    
     string OpenGate()
    {
    	cout << "You open the gate and step inside the yard. You marvel at the fact that even\n";
    	cout << "though this yard is tiny, there has apparently been no effort expended toward\n";
    	cout << "it's upkeep. You hear what could be muffled growling coming from your left.\n\n";
    
    	WhatToDo();
    	return 0;
    }
    
     string main()
    {	
    	char a;
    	cout << "You are standing in front of a run-down house. There is a blue sportscar that\n";
    	cout << "looks like it hasn't been driven in years parked out front. The car is covered\n";
    	cout << "in leaves. The house itself looks like it could collapse at any moment.\n";
    	cout << "There is a chain-link fence surrounding the house. The house has a screened-in\n";
    	cout << "porch, but you can't see much beyond that. Planted in the yard are trees which,\n";
    	cout << "if properly pruned, were probably nice bushes at one time. There is a closed\n";
    	cout << "gate which leads through the fence.\n\n";
    	
    		WhatToDo();
    		cin >> a;
    		if (a = "open gate")
    			OpenGate();
    	else cout << "Please enter a valid command.\n";
    
    	return 0;
    
    }
    Like I said, I'm new. I've been doing this for maybe two days. I'm taking baby steps here.
    you typed if( a="open gate") however this is syntax acceptable but probably not what you want. Change it to if( a == "open gate" ). = is the assignment operator, therefore you are assigned a the value of open gate and not checking if it is. I don't see any other problems as of now but I will continue looking.

  6. #6
    Registered User
    Join Date
    Oct 2005
    Posts
    5
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
     void WhatToDo()
    {
    	cout << "What do you want to do?\n\n";
    
    }
    
     void OpenGate()
    {
    	cout << "You open the gate and step inside the yard. You marvel at the fact that even\n";
    	cout << "though this yard is tiny, there has apparently been no effort expended toward\n";
    	cout << "it's upkeep. You hear what could be muffled growling coming from your left.\n\n";
    
    	WhatToDo();
    
    }
    
     int main()
    {	
    	string a;
    	cout << "You are standing in front of a run-down house. There is a blue sportscar that\n";
    	cout << "looks like it hasn't been driven in years, parked out front. The car is covered\n";
    	cout << "in leaves. The house itself looks like it could collapse at any moment.\n";
    	cout << "There is a chain-link fence surrounding the house. The house has a screened-in\n";
    	cout << "porch, but you can't see much beyond that. Planted in the yard are trees which,\n";
    	cout << "if properly pruned, were probably nice bushes at one time. There is a closed\n";
    	cout << "gate which leads through the fence.\n\n";
    	
    		WhatToDo();
    		cin >> a;
    		if (a="open gate")
    			OpenGate();
    	else cout << "Please enter a valid command.\n";
    
    	return 0;
    
    }
    My error message:
    Code:
    --------------------Configuration: Hello - Win32 Debug--------------------
    Compiling...
    Hello.cpp
    C:\Documents and Settings\Tarthus\Desktop\Hello.cpp(34) : error C2451: conditional expression of type 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' is illegal
            No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
    Error executing cl.exe.
    
    Hello.exe - 1 error(s), 0 warning(s)

  7. #7
    Bioport Productions
    Join Date
    Oct 2005
    Posts
    215
    change if( a = "open gate") to if( a == "open gate")

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    For now, change it to if (a=="open"), or it will never be true. Once you get it compiled and working, ask why.

  9. #9
    Registered User
    Join Date
    Oct 2005
    Posts
    5
    Quote Originally Posted by Daved
    For now, change it to if (a=="open"), or it will never be true. Once you get it compiled and working, ask why.
    Ok, it's compiled and working. Now, why do I have to leave off the "gate"?

  10. #10
    Bioport Productions
    Join Date
    Oct 2005
    Posts
    215
    Quote Originally Posted by Tarthus
    Ok, it's compiled and working. Now, why do I have to leave off the "gate"?
    I'm wondering the same thing

  11. #11
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    Because operator>> reads until it reaches whitespace (a tab, newline, or space character). So when the user types "open gate" into your program (without the quotes), cin will read until the first space and stop. So the word "open" will be stored in the variable a, and " gate" will be left in the input stream.

    I'd suggest using one-word commands for now. When you are ready for multiple word commands, or if you want to know now, you would use getline to read in a line of text:
    Code:
    getline(cin, a);
    This would read in the entire line, "open gate" and store it in the variable a. There are some slightly tricky issues with getline (not too hard, but they are there), which is why I suggest waiting to use it. But I'm sure once you do use it you'll find those things soon enough.

  12. #12
    Registered User
    Join Date
    Oct 2005
    Posts
    5
    Thanks, everyone. I'm sure I'll have more questions later.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What game programmer should I be? need some advice.
    By m3rk in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 04-21-2009, 12:12 AM
  2. When are you REALLY a programmer?
    By m.mixon in forum C Programming
    Replies: 5
    Last Post: 07-19-2006, 10:08 PM
  3. Senior 3D Programmer - Moab Studios™ LLC
    By moab in forum Projects and Job Recruitment
    Replies: 0
    Last Post: 08-30-2005, 11:19 PM
  4. Me as a programmer?
    By Cheeze-It in forum A Brief History of Cprogramming.com
    Replies: 12
    Last Post: 03-31-2002, 06:19 PM
  5. I need to interview professional programmer.....please
    By incognito in forum C++ Programming
    Replies: 1
    Last Post: 01-05-2002, 02:46 PM

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