Newbie with Very Newbie Question

This is a discussion on Newbie with Very Newbie Question within the C++ Programming forums, part of the General Programming Boards category; I'm just starting out with C++ coding and have a basic understanding of the language...though I still have a very ...

  1. #1
    Registered User
    Join Date
    Jun 2008
    Posts
    11

    Newbie with Very Newbie Question

    I'm just starting out with C++ coding and have a basic understanding of the language...though I still have a very limited range of "vocabulary and grammar" in C++.

    Right now I'm expanding on my little "Hello, World" tutorial program, and I can't figure out how to get the program to me saying "yes" to it. I've done this:

    if (cin>> "Yes")

    ...and also tried this:

    if (cin.get("Yes"))

    ...but got lengthy error reports for both. I'm trying to make two possible outcomes happen depending on whether the user inputs "yes" or "no." Right now I just need to figure out how to write it correctly and get the program to recognize it. I'm sure just about everyone on this forum knows the answer to my question.

    I'm here for help, not hurt. I've had bad experiences on a certain other C++ forum and don't want them repeated. If all you're going to do it shoot me down because I'm new, don't post on this thread at all.

  2. #2
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    Read the user input into a variable first, and then compare with "Yes"...

  3. #3
    Registered User
    Join Date
    Jun 2008
    Posts
    266
    Something like this...

    string str;
    getline(cin,str);
    if(str=="yes")

    you mean?

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,457
    Something like that.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Jun 2008
    Posts
    11
    Okay, I took medievalelk's advice and have come up with this new block of code:

    Code:
    	cout<< "Do you wanna see more?\n";
    	cin>> input;
    	if (input==1) {cout<< "Okay, here goes!\n";
    		for (x=0; x<10000; x++) {
    		cout<< x;}
    	}
    	if (input==0) cout<< "Awww...bye.\n";
    ...and it works perfectly. However, at the moment the user only has the option of responding in the primitive statements of affirmatives and negatives, 1 and 0. When I put "Yes" and "No" in their places, I get the following errors while debugging:

    -----------------------------------------------------
    c:\documents and settings\klope family\my documents\visual studio 2008\projects\helloworld\helloworld\hello world.cpp(35) : error C2446: '==' : no conversion from 'const char *' to 'int'
    There is no context in which this conversion is possible
    c:\documents and settings\klope family\my documents\visual studio 2008\projects\helloworld\helloworld\hello world.cpp(35) : error C2040: '==' : 'int' differs in levels of indirection from 'const char [4]'
    c:\documents and settings\klope family\my documents\visual studio 2008\projects\helloworld\helloworld\hello world.cpp(39) : error C2446: '==' : no conversion from 'const char *' to 'int'
    There is no context in which this conversion is possible
    c:\documents and settings\klope family\my documents\visual studio 2008\projects\helloworld\helloworld\hello world.cpp(39) : error C2040: '==' : 'int' differs in levels of indirection from 'const char [3]'
    -----------------------------------------------------

    I don't know what most of this means, but it is quite obvious by the scientific process that adding these values in quotes after my integer "input" (yes, it is an integer) caused all this. Of course, this is because "yes" and "no" aren't integers in any sense of the word. However, when I type "string str" below my other declared integers at the beginning of the block of code, it doesn't register "string" as a command in the STD namespace.

    Foolish question again, but how can you declare variables at the beginning/elsewhere in the block of code so that said variables can support letters in this kind of circumstance?

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,457
    Code:
    	cout<< "Do you wanna see more?\n";
    	cin>> input;
    	if (input==1)
    	{
    		cout<< "Okay, here goes!\n";
    		for (x=0; x<10000; x++)
    		{
    			cout<< x;
    		}
    	}
    	if (input==0) cout<< "Awww...bye.\n";
    Better code, although coding style is your own responsibility.
    Anyway, I don't really understand your problem at all.
    But I can give you hinters.
    C++ is strongly typed. Meaning that you read into an int for numbers and std::string for strings, etc. You also don't compare strings and integers.
    std::mystr = "mystr";
    mystr == 1 // ERROR
    mystr == "mystr" // OK
    int n = 1;
    n == 1; // OK
    n == "1"; // ERROR
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7

    Join Date
    Apr 2008
    Location
    USA
    Posts
    76
    Quote Originally Posted by Jedi_Mediator View Post
    ... how can you declare variables at the beginning/elsewhere in the block of code so that said variables can support letters in this kind of circumstance?
    If you want to support letters, you should declare your input as a (c-)string rather than an int.

    That is, a string or a C-String...
    Last edited by rudyman; 06-30-2008 at 11:03 AM.

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,457
    No, you should absolutely not (forget C!).
    std::string works for any string.
    short/int/long works for any integers.
    The real question is what you are trying to do in the first place.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User
    Join Date
    Jun 2008
    Posts
    11
    What I'm doing is to tell the program to offer the user the option of seeing another test of a FOR loop, which generates a huge block of numbers just as a novelty. Upon the program offering the user that option, the user should be able to say "Yes," in which case the block of numbers will appear, or "no," in which case the program will respond accordingly and then close.

    This is the error I got when adding "string input;" below my declared integers at the top of the code:
    --------------------------------------------------
    binary '==': no operator found which takes a left-hand operand of type 'std::string' (or there is no acceptable conversion)
    --------------------------------------------------
    I also got an error message claiming the same about my line "cin>> input;", excepting that it spoke of a right-hand operand instead of left-hand and complained about the 'binary >>'.

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    Feel free to show the whole code that has the problem. If input is a string, then you can't compare it to 1, you have to compare it to "1", because 1 is an int and "1" is a (C style) string.

    Also, if you're using strings, don't forget to #include <string>.
    Last edited by Daved; 06-30-2008 at 11:40 AM.

  11. #11
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,457
    And I have also told you that you cannot compare integers to strings directly because C++ is strongly typed.
    Compare strings to strings and compare integers to integers.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Registered User
    Join Date
    Jun 2008
    Posts
    11
    I didn't think I was comparing integers to strings, Elysia. I'm trying to use "Yes" instead of "1" and "No" instead of "0".

    Daved: hmm. I've seen games use .str files before. Is that what I need to use?

  13. #13
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> I've seen games use .str files before. Is that what I need to use?
    I don't know what you mean by this, but I don't think it has anything to do with what I said.

    My comment about #include <string> was that you need to add #include <string> to the top of your source file(s) that uses the string class just like you have #include <iostream>.

  14. #14
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,457
    Quote Originally Posted by Jedi_Mediator View Post
    I didn't think I was comparing integers to strings, Elysia. I'm trying to use "Yes" instead of "1" and "No" instead of "0".
    That's the problem. I don't see the code so I can't say what you're doing wrong.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #15
    Registered User
    Join Date
    Jun 2008
    Posts
    11
    Well, just for further reference, then, here's my full, rather convoluted code, as of right now.

    Code:
    #include "Hello World.h" /*References "Hello World.h" header, which contains the 
    instruction to use the <iostream> "dictionary" of commands*/ 
    using namespace std; /*Tells the program to use the standard (std) library of 
    					 commands, enabling things like "cout" and "cin.get" to be
    					 used without the "std::" prefix preceding them*/ 
    #include "Hello World.txt"
    
    int main (int argc, char* argv[]) /*Unknown as of yet*/ 
    {
    	int age;
    	int x;
    
    	cout<<"Hello, World!\n"; /*Outputs "Hello, World!" in the prompt window*/
    	//cout<<argv[0]; /*Results in the display of this program's directory, in 
    	//				sequence after the display of "Hello, World!"*/
    	//cout<<argv[1];
    	//cout<<argv[2];
    	cout<< "How old am I?\n";
    	cin>> age;
    	cin.get();
    	if (age < 10) cout<< "Wow, I'm young!\n";
    		else cout<< "Wow, I'm old!\n";
    	//		if (age > 100) cout<< "Are you serious? Am I really that ancient?\n";
    	cin.get();
    	cout<< "Here goes my looping test! Are you ready?\n";
    	cin.get();
    	{
    		for (x=0; x<1000; x++) {
    			cout<< x;}
    	}
    	cout<< "Isn't that crazy?\n";
    	cin.get();
    	cout<< "Do you wanna see more?\n";
    	cin>> input;
    	if (input=="yes") {cout<< "Okay, here goes!\n";
    		for (x=0; x<10000; x++) {
    		cout<< x;}
    	}
    	if (input=="no") cout<< "Awww...bye.\n";
    	cin.get(); /*Must press enter to close program*/
    	return 0; /*Return the value of 0 to the OS upon program's success - a verification*/
    }
    Daved: .str stands for string. My problem was that, when I typed in #include input at the very top of my code, it gave me an error message that said a directory or filename was expected, and instead an 'identifier' was found. This led me to believe that #include is meant to point to a file or directory (which I found that I was already aware of when I noticed the very first line of my code), which led me to believe that the file in question might need to be a .str file like I mentioned. Now, are you saying that I type in #include <string> literally, instead of replacing that "<string>" with a file or directory name? Somehow my brain registered that you intended the latter.


    EDIT: Thank you Daved, your advice worked. I type in #include <string> where it belonged and I can now make my programs accept Yes and No responses. Thanks, everybody.
    Last edited by Jedi_Mediator; 06-30-2008 at 06:53 PM.

Page 1 of 2 12 LastLast
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, 05:45 PM
  3. a stupid question from a newbie
    By newcomer in forum C++ Programming
    Replies: 4
    Last Post: 01-11-2003, 03:38 PM
  4. confusion with integers (newbie question)
    By imortal in forum C Programming
    Replies: 7
    Last Post: 12-06-2002, 03:09 PM
  5. newbie class templates question
    By daysleeper in forum C++ Programming
    Replies: 2
    Last Post: 09-18-2001, 09:50 AM

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