Thread: Oh god help me

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    3

    Oh god help me

    So I'm in class right now, using Visual C++ 6. I'm so close to finishing this module, but I need to know how to make a variable that holds two characters. What the goal of the program is, is to take the users province in two characters, then determine which area it's in (ie. Western provinces). I don't need anything too hardcore, because this is just supposed to b a simple project.

    Code:
    #include <iostream.h>
    #include <string.h>
    
    void main() {
    
    	char sProv;
    
    	cout << "Please enter you Province: ";
    	cin >> sProv;	//Get the province that the usProver isProv from	
    	cout << sProv;
    
    	if (sProv == 'ab') {
    		cout << "\nYou're in the Western provinces.\n" << endl;
    	}
    	else if (sProv == 'on') {
    		cout << "\nYou're in the Eastern provinces.\n" << endl;
    	}
    	else if (sProv == 'nb') {
    		cout << "\nYou're in the Atlantic provinces.\n" << endl;
    	}	
    }
    This here doesn't work. All it does it gets the first character, obviously, but I don't know how to create strings or modify the char variable if that's what's needed. If you help fast, maybe I can start the next module today :P

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    The proper way to do this in modern C++ is to use the C++ string class. However, your code uses out-dated and non-standard headers, which makes me think that maybe your class instructor doesn't know or doesn't want you to know how to write standard C++ code.

    One simple solution that doesn't involve learning strings is to read in the two characters separately, one at a time, into separate variables. If the first character matches the first character of any of your provinces, then check the second character to see if it also matches.

  3. #3
    C++ Enthusiast jmd15's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    532
    Instead of this:
    Code:
    char sProv;
    Do this:
    Code:
    char sProv[256]={0};
    Now sProv can hold a lot more characters and it will read more than just one character. Try it out.

    EDIT:
    Oh yeah, the part where you have:
    Code:
    if(sProv=='ab')
    Should be:
    Code:
    if(sProv=="ab")
    You need that because what you are doing is making it think it's comparing a single char instead of more than one char.
    Trinity: "Neo... nobody has ever done this before."
    Neo: "That's why it's going to work."
    c9915ec6c1f3b876ddf38514adbb94f0

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> if(sProv=="ab")

    That won't work. You cannot compare C-style strings with ==.

  5. #5
    Registered User
    Join Date
    Nov 2005
    Posts
    3
    Oo, okay. Well, I tryed your answer jmd, however now I'm getting the error:

    Code:
    error C2446: '==' : no conversion from 'const int' to 'char *'
            Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
    So when I take away the second '=', I get:

    Code:
    error C2440: '=' : cannot convert from 'const int' to 'char [256]'
            There are no conversions to array types, although there are conversions to references or pointers to arrays
    Any ideas?

  6. #6
    C++ Enthusiast jmd15's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    532
    if(strcmp(sProv,"ab")==0)

    EDIT:
    If you're doing this "the C way" then this should be in the C Board. In C++ you would declare a string, store their input in the string and compare the string.
    Last edited by jmd15; 11-01-2005 at 03:37 PM.
    Trinity: "Neo... nobody has ever done this before."
    Neo: "That's why it's going to work."
    c9915ec6c1f3b876ddf38514adbb94f0

  7. #7
    Registered User
    Join Date
    Nov 2005
    Posts
    3
    You...

    ...are a god! Thank you so much .

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. what race is god?
    By Leeman_s in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 02-22-2004, 05:38 PM
  2. God II
    By Leeman_s in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 01-09-2003, 01:42 AM
  3. GOD and religion
    By Unregistered in forum A Brief History of Cprogramming.com
    Replies: 19
    Last Post: 10-14-2001, 05:13 PM
  4. Foundations
    By mithrandir in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 10-05-2001, 02:18 PM