Follow us on Twitter!
Things are more like they are now than they have ever been before. - Dwight D. Eisenhower
Wednesday, April 23, 2014
Navigation
Home
HellBoundHackers Main:
HellBoundHackers Find:
HellBoundHackers Information:
Learn
Communicate
Submit
Shop
Challenges
HellBoundHackers Exploit:
HellBoundHackers Programming:
HellBoundHackers Think:
HellBoundHackers Track:
HellBoundHackers Patch:
HellBoundHackers Other:
HellBoundHackers Need Help?
Other
Members Online
Total Online: 18
Guests Online: 17
Members Online: 1

Registered Members: 82885
Newest Member: ConiBE
Latest Articles
View Thread

HellBound Hackers | Computer General | Programming

Author

c++ switch problems


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 19-02-11 22:01
I cant figure what is wrong. I am guessing Devc++ doesnt like my or statements in my Hours. Anyone know how to fix it? Error says duplicate case value


Code


#include <iostream>
#include <string>
using namespace std;

int main ()
{
    string hourtext;
    string minute;
    string AMPM;
    string choice;
    int hour;
   
   
    do
    {
    cout <<"Enter the time in astronomical form seperating hour and minutes by a space (i.e. 14 01): " << endl;
    cin >> hour >> minute;
   
    switch (hour)
    {
           case ("1" || "13"): hourtext = "One";
           break;
           
           case ("2" || "14"): hourtext = "Two";
           break;
           
           case ("3" || "15"): hourtext = "Three";
           break;
           
           case ("4" || "16"): hourtext = "Four";
           break;
           
           case ("5" || "17"): hourtext = "Five";
           break;
           
           case ("6" || "18"): hourtext = "Six";
           break;
           
           case ("7" || "19"): hourtext = "Seven";
           break;
           
           case ("8" || "20"): hourtext = "Eight";
           break;
           
           case ("9" || "21"): hourtext = "Nine";
           break;
           
           case ("10" || "22"): hourtext = "Ten";
           break;
           
           case ("11" || "23"): hourtext = "Eleven";
           break;
           
           case ("12"):
                if (minute == "00")
                hourtext = "Noon";
                minute = "";
                AMPM = "";
           break;
           
           case ("24" || "0"):
                if (minute == "00")
                hourtext = "Midnight";
                minute = "";
                AMPM = "";
           break;

           default : cout << "That is not a valid Hour" << endl;
                   hourtext = "";
                   AMPM = "";
    }                                             // hour switch   
   
    switch (minute)
    {
           case ("01"): minute = "O' One";
           break;
           
           case ("02"): minute = "O' Two";
           break;
   
           case ("03"): minute = "O' Three";
           break;
           
           case ("04"): minute = "O' Four";
           break;
           
           case ("05"): minute = "O' Five";
           break;
           
           case ("06"): minute = "O' Six";
           break;
           
           case ("07"): minute = "O' Seven";
           break;
           
           case ("08"): minute = "O' Eight";
           break;
           
           case ("09"): minute = "O' Nine";
           break;
           
           case ("10"): minute = "Ten";
           break;
           
           case ("11"): minute = "Eleven";
           break;
           
           case ("12"): minute = "Twelve";
           break;
           
           case ("13"): minute = "Thirteen";
           break;
           
           case ("14"): minute = "Fourteen";
           break;
           
           case ("15"): minute = "Fifteen";
           break;
           
           case ("16"): minute = "Sixteen";
           break;
           
           case ("17"): minute = "Seventeen";
           break;
           
           case ("18"): minute = "Eighteen";
           break;
           
           case ("19"): minute = "Ninteen";
           break;
           
           case ("20"): minute = "Twenty";
           break;
           
           case ("21"): minute = "Twenty One";
           break;
           
           case ("22"): minute = "Twenty Two";
           break;
           
           case ("23"): minute = "Twenty Three";
           break;
           
           case ("24"): minute = "Twenty Four";
           break;
           
           case ("25"): minute = "Twenty Five";
           break;
           
           case ("26"): minute = "Twenty Six";
           break;
           
           case ("27"): minute = "Twenty Seven";
           break;
           
           case ("28"): minute = "Twenty Eight";
           break;
           
           case ("29"): minute = "Twenty Nine";
           break;
           
           case ("30"): minute = "Thirty";
           break;
           
           case ("31"): minute = "Thirty One";
           break;
           
           case ("32"): minute = "Thirty Two";
           break;
           
           case ("33"): minute = "Thirty Three";
           break;
           
           case ("34"): minute = "Thirty Four";
           break;
           
           case ("35"): minute = "Thirty Five";
           break;
           
           case ("36"): minute = "Thirty Six";
           break;
           
           case ("37"): minute = "Thirty Seven";
           break;
           
           case ("38"): minute = "Thirty Eight";
           break;
           
           case ("39"): minute = "Thirty Nine";
           break;
           
           case ("40"): minute = "Fourty";
           break;
           
           case ("41"): minute = "Fourty One";
           break;
           
           case ("42"): minute = "Fourty Two";
           break;
           
           case ("43"): minute = "Fourty Three";
           break;
           
           case ("44"): minute = "Fourty Four";
           break;
           
           case ("45"): minute = "Fourty Five";
           break;
           
           case ("46"): minute = "Fourty Six";
           break;
           
           case ("47"): minute = "Fourty Seven";
           break;
           
           case ("48"): minute = "Fourty Eight";
           break;
           
           case ("49"): minute = "Fourty Nine";
           break;
           
           case ("50"): minute = "Fifty";
           break;
           
           case ("51"): minute = "Fifty One";
           break;
           
           case ("52"): minute = "Fifty Two";
           break;
           
           case ("53"): minute = "Fifty Three";
           break;
           
           case ("54"): minute = "Fifty Four";
           break;
           
           case ("55"): minute = "Fifty Five";
           break;
           
           case ("56"): minute = "Fifty Six";
           break;
           
           case ("57"): minute = "Fifty Seven";
           break;

           case ("58"): minute = "Fifty Eight";
           break;
           
           case ("59"): minute = "Fifty Nine";
           break;
           
           default : cout << "Not a vaild minute" << endl;
                   hourtext = "";
                   minute = "";
                   AMPM = "";
    }                                          // minute switch
   
    if (hour < 12 || hour == 23 || hour == 0)
    {
             AMPM = "AM";
             }
             else AMPM = "PM";
             
    cout << "Would you like to input another time (yes or no): ";
    cin >> choice;
   
   
    } while (choice == "yes");                    // do while
   
   
    system("pause");
    return 0;
}








Author

RE: c++ switch problems

stealth-
Member



Posts: 1003
Location: Eh?
Joined: 10.04.09
Rank:
Mad User
Posted on 19-02-11 22:23
I'm know more about C than I do C++, but I think I know what you're problem is (anyone feel free to correct me). Your first case ("1" || "13") doesn't actually mean "if the variable is "1" or "13", run this". I believe it instead means something more like this: "If "1" is true or if "13" is true, run this".

So basically, instead of:

Code
("1" || "13")




Your code is interpreted literally like this:

Code
("1" != 0 || "13" != 0)




Which, since populated strings are always 1, or True, you're case simplifies to this:

Code
(1||1)




Since all of your cases simplify to True, aka 1, then you have duplicate cases of the same value and the compiler raises an error.


The irony of man's condition is that the deepest need is to be free of the anxiety of death and annihilation; but it is life itself which awakens it, and so we must shrink from being fully alive.
http://www.stealt. . .

Edited by stealth- on 19-02-11 22:24
http://www.stealth-x.com
Author

RE: c++ switch problems

GTADarkDude
Member



Posts: 142
Location: The Netherlands
Joined: 23.02.08
Rank:
Newbie
Posted on 19-02-11 22:26
First thing:
There is no such thing as a switch for strings.

Second:
Code
case (2 || 3):
    //rest of code



should be written as
Code
case 2:
case 3:
    //rest of code



With 2 || 3, you're evaluating this first, ending up with a boolean value. Then this is interpreted as an integer, but this is definitely not what you want.

--------

EDIT: Irrelevant info removed. Just use integers.


...

Edited by GTADarkDude on 19-02-11 22:47
- - -
Author

RE: c++ switch problems


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 19-02-11 22:34
so i need to add seperate ones for 13-23 and it will fix it? I will test that out.


Author

RE: c++ switch problems

GTADarkDude
Member



Posts: 142
Location: The Netherlands
Joined: 23.02.08
Rank:
Newbie
Posted on 19-02-11 22:50
vegeta_man111 wrote:
so i need to add seperate ones for 13-23 and it will fix it? I will test that out.
You'll also have to remove all quotes around the numbers, and make minutes of type int.


...
- - -
Author

RE: c++ switch problems


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 19-02-11 23:14
fixed. thanks guyf for your help. I appreciate having someone who I can thoroughly count on.

Code


// Chapter X Assignment X
// Corey Hartshorn

#include <iostream>
#include <string>
using namespace std;

int main ()
{
    string hourtext;
    string minutetext;
    string AMPM;
    string choice;
    int hour;
    int minute;
   
   
    do
    {
    cout <<"Enter the time in astronomical form seperating hour and minutes by a space (i.e. 14 01): " << endl;
    cin >> hour >> minute;
   
     if (hour < 12 || hour == 24 || hour == 0)
    {
             AMPM = "AM";
             }
             else AMPM = "PM";
   
    switch (hour)
    {
           case 1: hourtext = "One";
           break;
           
           case 2: hourtext = "Two";
           break;
           
           case 3: hourtext = "Three";
           break;
           
           case 4: hourtext = "Four";
           break;
           
           case 5: hourtext = "Five";
           break;
           
           case 6: hourtext = "Six";
           break;
           
           case 7: hourtext = "Seven";
           break;
           
           case 8: hourtext = "Eight";
           break;
           
           case 9: hourtext = "Nine";
           break;
           
           case 10: hourtext = "Ten";
           break;
           
           case 11: hourtext = "Eleven";
           break;
           
           case 12:
                if (minute == 0)
                {
                hourtext = "Noon";
                minutetext = "";
                AMPM = "";
                break;
                }
                else hourtext = "Twelve";
           break;
           
           case 13: hourtext = "One";
           break;
           
           case 14: hourtext = "Two";
           break;
           
           case 15: hourtext = "Three";
           break;
           
           case 16: hourtext = "Four";
           break;
           
           case 17: hourtext = "Five";
           break;
           
           case 18: hourtext = "Six";
           break;
           
           case 19: hourtext = "Seven";
           break;
           
           case 20: hourtext = "Eight";
           break;
           
           case 21: hourtext = "Nine";
           break;
           
           case 22: hourtext = "Ten";
           break;
           
           case 23: hourtext = "Eleven";
           break;
           
           case 24:
                if (minute == 0)
                {
                hourtext = "Midnight";
                minutetext = "";
                AMPM = "";
                break;
                }
                else hourtext = "Twelve";
           break;
           
           case 0:
                if (minute == 0)
                {
                hourtext = "Midnight";
                minutetext = "";
                AMPM = "";
                break;
                }
                else hourtext = "Twelve";
           break;

           default : cout << "That is not a valid Hour" << endl;
                   hourtext = "";
                   AMPM = "";
    }                                             // hour switch   
   
    switch (minute)
    {
           case 0: minutetext = "";
                   AMPM = "";
           break;
           
           case 1: minutetext = "O' One";
           break;
           
           case 2: minutetext = "O' Two";
           break;
   
           case 3: minutetext = "O' Three";
           break;
           
           case 4: minutetext = "O' Four";
           break;
           
           case 5: minutetext = "O' Five";
           break;
           
           case 6: minutetext = "O' Six";
           break;
           
           case 7: minutetext = "O' Seven";
           break;
           
           case 8: minutetext = "O' Eight";
           break;
           
           case 9: minutetext = "O' Nine";
           break;
           
           case 10: minutetext = "Ten";
           break;
           
           case 11: minutetext = "Eleven";
           break;
           
           case 12: minutetext = "Twelve";
           break;
           
           case 13: minutetext = "Thirteen";
           break;
           
           case 14: minutetext = "Fourteen";
           break;
           
           case 15: minutetext = "Fifteen";
           break;
           
           case 16: minutetext = "Sixteen";
           break;
           
           case 17: minutetext = "Seventeen";
           break;
           
           case 18: minutetext = "Eighteen";
           break;
           
           case 19: minutetext = "Ninteen";
           break;
           
           case 20: minutetext = "Twenty";
           break;
           
           case 21: minutetext = "Twenty One";
           break;
           
           case 22: minutetext = "Twenty Two";
           break;
           
           case 23: minutetext = "Twenty Three";
           break;
           
           case 24: minutetext = "Twenty Four";
           break;
           
           case 25: minutetext = "Twenty Five";
           break;
           
           case 26: minutetext = "Twenty Six";
           break;
           
           case 27: minutetext = "Twenty Seven";
           break;
           
           case 28: minutetext = "Twenty Eight";
           break;
           
           case 29: minutetext = "Twenty Nine";
           break;
           
           case 30: minutetext = "Thirty";
           break;
           
           case 31: minutetext = "Thirty One";
           break;
           
           case 32: minutetext = "Thirty Two";
           break;
           
           case 33: minutetext = "Thirty Three";
           break;
           
           case 34: minutetext = "Thirty Four";
           break;
           
           case 35: minutetext = "Thirty Five";
           break;
           
           case 36: minutetext = "Thirty Six";
           break;
           
           case 37: minutetext = "Thirty Seven";
           break;
           
           case 38: minutetext = "Thirty Eight";
           break;
           
           case 39: minutetext = "Thirty Nine";
           break;
           
           case 40: minutetext = "Fourty";
           break;
           
           case 41: minutetext = "Fourty One";
           break;
           
           case 42: minutetext = "Fourty Two";
           break;
           
           case 43: minutetext = "Fourty Three";
           break;
           
           case 44: minutetext = "Fourty Four";
           break;
           
           case 45: minutetext = "Fourty Five";
           break;
           
           case 46: minutetext = "Fourty Six";
           break;
           
           case 47: minutetext = "Fourty Seven";
           break;
           
           case 48: minutetext = "Fourty Eight";
           break;
           
           case 49: minutetext = "Fourty Nine";
           break;
           
           case 50: minutetext = "Fifty";
           break;
           
           case 51: minutetext = "Fifty One";
           break;
           
           case 52: minutetext = "Fifty Two";
           break;
           
           case 53: minutetext = "Fifty Three";
           break;
           
           case 54: minutetext = "Fifty Four";
           break;
           
           case 55: minutetext = "Fifty Five";
           break;
           
           case 56: minutetext = "Fifty Six";
           break;
           
           case 57: minutetext = "Fifty Seven";
           break;

           case 58: minutetext = "Fifty Eight";
           break;
           
           case 59: minutetext = "Fifty Nine";
           break;
           
           default : cout << "Not a vaild minute" << endl;
                   hourtext = "";
                   minutetext = "";
                   AMPM = "";
    }                                          // minute switch
   
    cout << hourtext << " " << minutetext << " " << AMPM << endl;         
    system("pause");
   
    cout << "Would you like to input another time (yes or no): ";
    cin >> choice;
   
   
    } while (choice == "yes");                    // do while
    return 0;
}








Author

RE: c++ switch problems


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 19-02-11 23:14
So am I the only one who thinks that something like, let's say, an array of strings would be better suited for this where you can just pop in whatever value you have in the index? You know, modulo 12 could be used for hours? And then it could also have something at positions 0 since the current code wants to tell you that there's no such thing as 0 minutes? No? OK, moving on then, never mind me.


Author

RE: c++ switch problems

GTADarkDude
Member



Posts: 142
Location: The Netherlands
Joined: 23.02.08
Rank:
Newbie
Posted on 19-02-11 23:18
Of course you're not the only one. It's just that I don't want to advise him to use anything (arrays) he has never seen before, before he understands basic control structures such as switch. Oh and I did mention using % 12 in my previous post, but I just noticed I edited that out by accident. -_-"

Oh and vegata_man, when two cases in your switch statement have similar bodies, you can type it like this:
Code
case 2:
case 14:
    //code


This is called a fallthrough switch.


...

Edited by GTADarkDude on 19-02-11 23:22
- - -
Author

RE: c++ switch problems


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 19-02-11 23:27
GTADarkDude wrote:
Of course you're not the only one.
...
Oh and vegata_man, when two cases in your switch statement have similar bodies, you can type it like this:
Code
case 2:
case 14:
    //code


This is called a fallthrough switch.

Spoke my mind for me there, was just gonna say.
wuvs ya <3 :)


Author

RE: c++ switch problems


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 20-02-11 05:18
I appreciate the info. I didnt know how to do the fallthrough switch, but now that I do I can use it. And I appreciate not using something "new" to me to get my code working more productive. I am only in my basic c++ class and have 4 chapters to go before advanced.


Author

RE: c++ switch problems


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 20-02-11 07:35
Instead of hardcoding those values for hours and minutes, why not implement something like:
Code

string ones= {"one", "two". "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve"};
string tens = {"teen", "twenty", "thirty", "forty", "fifty", "sixty"};
switch(hour%12){
      case 0:
             if(minutes == 0)
                   minutetext="";
                   if(hour == 12)
                         hourtext="Noon";
                   else
                         hourtext="Midnight";
             else
                   hourtext = ones[(hour%12)-1];
      default:
             hourtext = ones[(hour%12)-1];
}
switch(minutes/10){
      case 0:
             if(minutes == 0)
             minutetext = "";
             else
             minutetext=ones[minutes-1];
      break;
      case 1:
             if(minutes == 10)
             minutetext = "ten";
             else if(minutes == 13)
             minutetext = "thirteen";
             else if(minutes == 15)
             minutetext = "fifteen";
             else if(minutes == 11||12)
             minutetext = ones[minutes-1];
             else
             minutetext = ones[minutes%10] + tens[0];
             break;
      default:
           minutetext = tens[(minutes/10)-1] + ones[(minutes%10)];
           break;
}








Edited by on 20-02-11 08:01
Author

RE: c++ switch problems


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 20-02-11 07:58
Oooh, missing semicolons, lack of brackets, I've been programming in Python too long...You should be able to make heads and tails of it though because I always indent Wink
Anyway, since both non-military time and hours work on a "circular" principle, we can use the modulo operator here. The modulo operator takes the whole number remainder and returns it rather than the actual quotient, which is of course what we want. With hours, the 0 modulo (all whole number multiples of 12, and 0) is trickiest to handle, so it gets its own code. The rest can be generalized, which we will use the default case for.
With minutes, the same is done, except the tens case is more nuanced, so it gets its own case. Instead of hardcoding every single value, we merely take advantage of English's consistent (enough) nature.