Switch

Can't get switch to recognize a string

Page 1 of 1

9 Replies - 793 Views - Last Post: 07 January 2010 - 11:18 PM Rate Topic: -----

#1 xxsssupermanxx  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 07-January 10

Switch

Posted 07 January 2010 - 09:52 PM

Hello. I'm somewhat new to c++ programming. I can make very basic programs. I was just wondering if the switch function can recognize strings? I use "apstring.h" and "apstring.cpp" for my strings, I don't know how to take user input and initialize it as a string unless it is an "apstring".

at any rate, here is a little bit of the code i've written, if anyone has any suggestions:

int main()
{
	apstring KEY;
	int i = 0;
		cout << "Please enter a keyword: ";
		getline(cin, KEY);
		system("cls");
	
	do
	{
		switch(KEY)
				{
					 case "Word":
							 Word();
					 break;
					 default:
							  cout << "That wasn't a keyword" << endl;
					 break;
				}
	}while(i == 0);

	return 0;
}



my compiler is telling me that "KEY" should be an integer... I don't know what to do!

** Edit ** :code:

This post has been edited by xxsssupermanxx: 07 January 2010 - 10:40 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Switch

#2 seeP+  Icon User is offline

  • D.I.C Addict

Reputation: 55
  • View blog
  • Posts: 601
  • Joined: 20-July 09

Re: Switch

Posted 07 January 2010 - 10:28 PM

Is this an object for a class?
apstring KEY;



If not declare KEY as a string.
Was This Post Helpful? 0
  • +
  • -

#3 Guest_c.user*


Reputation:

Re: Switch

Posted 07 January 2010 - 10:31 PM

"Word"
is transformed to the address of the static string (every string has an unique address in the static area)
the switch's case should have an integer value like a number or a character constant (which will be converted to an integer)
Was This Post Helpful? 0

#4 no2pencil  Icon User is offline

  • Admiral Fancy Pants
  • member icon

Reputation: 5379
  • View blog
  • Posts: 27,350
  • Joined: 10-May 07

Re: Switch

Posted 07 January 2010 - 10:34 PM

I have two questions about your code :

1.) Why are you issuing a switch & only have one case?

2.) How are you exiting a while loop when the value of i is not changed in the loop?
Was This Post Helpful? 0
  • +
  • -

#5 xxsssupermanxx  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 07-January 10

Re: Switch

Posted 07 January 2010 - 10:36 PM

In my Computer Programming class, we only used "apstring.h" and "apstring.cpp" for strings, I don't know how to utilize the string function.

-I'm only using one case because that was just an example.
-I wanted an infinite loop, and that's the best way I know how to do it. If you have any better ways, please let me know =]

This post has been edited by xxsssupermanxx: 07 January 2010 - 10:38 PM

Was This Post Helpful? 0
  • +
  • -

#6 seeP+  Icon User is offline

  • D.I.C Addict

Reputation: 55
  • View blog
  • Posts: 601
  • Joined: 20-July 09

Re: Switch

Posted 07 January 2010 - 10:47 PM

  i = 0;
		cout << "Please enter a keyword: ";
		getline(cin, KEY);
		system("cls");
   
	do
	{
		switch(KEY)
				{
					 case "Word":
							 Word();



So you have a header file that deals with strings. You create an object of the file but I don't see where you call it.... Then with the object(KEY) you use it to store the users input. In your switch Word() is supposed to be called only if KEY = "Word";. How is the user supposed to know to enter "Word"?
Was This Post Helpful? 0
  • +
  • -

#7 xxsssupermanxx  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 07-January 10

Re: Switch

Posted 07 January 2010 - 10:53 PM

Haha, the program is meant to be used by someone that I tell keywords to, in the case that some other nosey person finds it.

I'm not worried about HOW they should find the "keyword" or whatnot. I'm just looking for someone that may be familiar with the apstring class and can help me understand why the switch function isn't recognizing it.

I do believe that I've used the apstring class with the switch function before, but for some reason I can't seem to get it to work here.
Was This Post Helpful? 0
  • +
  • -

#8 jimblumberg  Icon User is offline

  • member icon


Reputation: 4220
  • View blog
  • Posts: 13,229
  • Joined: 25-December 09

Re: Switch

Posted 07 January 2010 - 11:04 PM

A switch statement will not work with a string it requires an int.

Jim
Was This Post Helpful? 0
  • +
  • -

#9 seeP+  Icon User is offline

  • D.I.C Addict

Reputation: 55
  • View blog
  • Posts: 601
  • Joined: 20-July 09

Re: Switch

Posted 07 January 2010 - 11:05 PM

Ok so the apstring class access a function that returns a strings? So that the switch can use it as a case. From what I know you can only use a switch as a int or char. So you could use a string variable string[subscript].
Was This Post Helpful? 0
  • +
  • -

#10 David W  Icon User is offline

  • DIC supporter
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,788
  • Joined: 20-September 08

Re: Switch

Posted 07 January 2010 - 11:18 PM

You might like to take a look at this ...

http://www.codeguru....icle.php/c4067/

Or for a quick 'work-around' to switch on string input see this:

#include <iostream>
#include <string>

using namespace std;

const string SW_STR[] = { "w1", "w2", "w3" };

int getPosition(const string& testStr )
{
    for( unsigned i=0; i< sizeof(SW_STR)/sizeof(SW_STR[0]) ; ++i )
        if( SW_STR[i] == testStr )
            return i+1;
    return 0; // ie. NOT found
}

int main()
{
    int i = 0;
    do
    {
        cout << "Enter one of w1, w2, w3 : " << flush;
        string testStr;
        getline( cin, testStr );
        i = getPosition( testStr );
        switch(i)
        {
            case 1:  cout << "word one";    break;
            case 2:  cout << "word two";    break;
            case 3:  cout << "word three";  break;
            default: cout << "That wasn't a keyword";
        }
        cout << endl;
    }while(i);

    cout << "\n\nPress 'Enter' to continue ... " << flush;
    cin.get();
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1