Page 1 of 2 1 2 LastLast
Results 1 to 20 of 31

Thread: c++ switch statement     submit to reddit submit to twitter

  1. #1
    Cerberus
    Join Date
    Oct 2008
    Posts
    436
    BG Level
    4

    c++ casing function

    instead of making a new topic i decided to just edit a old one. What i need to do is take the first letter of each string read from a input file and upper case it

    ex:

    start: i sparkle when i go out into the sun.
    after: I sparkle when i go out into the sun.

    I have the input/output done just need a little help with the function to convert the first object in the string.

    Code:
    #include <iostream>
    #include <fstream>
    
    using namespace std;
    
    
    void initialCap(&string); {
    
    }
    
    
    int main() {
    	string line;
    	ifstream input ("input.txt");
    	ofstream outfile ("output.txt");
    	if (input.is_open()) {
    		while (!input.eof()) {
    			getline (input,line);
    			cout << line << endl;
    			outfile << line << endl;
    		}
    		input.close();
    	}
    	
    	else
    		cout << "This file does not exist";
    	
    	return 0;
    }
    I found this code, would using it to the trick?

    Code:
    string initialCap(string str) {
    	string::iterator it(str.begin());
    	if (it != str.end())
    		str[0] = toupper((unsigned char)str[0]);
    			while(++it != str.end()) {
    				*it = tolower((unsigned char)*it);
    			}
    	return str;
    }

  2. #2
    Salvage Bans
    Join Date
    Feb 2007
    Posts
    811
    BG Level
    5
    FFXIV Character
    Orinthia Warsong
    FFXIV Server
    Excalibur
    FFXI Server
    Bahamut

    Maybe set up some if statements before hand to set your letter grade to the right letter based on the grade number? I think switch/case statement are just for that specific value only, though you could have some fun by setting up a case for each individual value and setting the breaks at the right intervals. Would be messy/pointless though like that.

  3. #3
    Naver
    Guest

    Seems like a little extra work, but I would just check before hand the "grade" and set that variable myLetterGrade to the corresponding letter using if and else ifs.

    ie.

    percentage variable used for checking grade...
    if( percentage > 90 ) myLetterGrade = 'A';
    else if( percentage > 80 ) myLetterGrade = 'B';

    etc..

    This way the switch statement always gets your case and Won't fall into the default.

  4. #4
    Cerberus
    Join Date
    Oct 2008
    Posts
    436
    BG Level
    4

    oh that was a good idea. I'll do a set of if statements before the swich to set it to a defalt value. Thanks

  5. #5
    Naver
    Guest

    No problem.

  6. #6
    Cerberus
    Join Date
    Oct 2008
    Posts
    436
    BG Level
    4

    What is wrong with this, it keeps printing "F"

    Code:
    #include <iostream>
    
    using namespace std;
    
    void main() {
    	int choice;
    	int myGrade = 75;
    
    	if (myGrade >= 90) {
    		choice = '1'; 
    	}
    	else if (myGrade >= 80 ) { 
    		choice = '2'; 
    	}
    	else if (myGrade >= 70) { 
    		choice = '4' ; 
    	}
    	else if (myGrade >= 60) { 
    		choice = '4';  
    	}
    	else {  
    		choice = '5';  
    	} 
    
    		
    	switch (choice) {
    		case 1:
    			cout << "A" << endl;
    			system ("Pause");
    			break;
    		case 2:
    			cout << "B" << endl;
    			system ("Pause");
    			break;
    		case 3:
    			cout << "C" << endl;
    			system ("Pause");
    			break;
    		case 4:
    			cout << "D" << endl;
    			system ("Pause");
    			break;
    		default:
    			cout << "F" << endl;
    			system ("Pause");
    	}
    }

    edit: nvm fixed it didnt need the ' ' around the numbers

  7. #7
    Naver
    Guest

    ' ' treat anything inside as a char

    Edit: Actually I'll rephrase that, the reason you were getting F is because the ascii value of '1' is 49. Careful when wrapping numbers or letters around ' '.

  8. #8
    Fake Numbers
    Join Date
    Jul 2008
    Posts
    90
    BG Level
    2

    You don't really need the switch statement at all here, you could just:

    Code:
    #include <iostream>
    
    using namespace std;
    
    void main() {
    	int myGrade = 75;
    
    	if (myGrade >= 90) {
    		cout << 'A' << endl;
    	}
    	else if (myGrade >= 80 ) { 
    		cout << 'B' << endl;
    	}
    	else if (myGrade >= 70) { 
    		cout << 'C' << endl;
    	}
    	else if (myGrade >= 60) { 
    		cout << 'D' << endl;
    	}
    	else {  
    		cout << 'F' << endl;
    	} 
            system("pause");
    }

  9. #9
    Murder machine with a motor in her nose
    Join Date
    Apr 2007
    Posts
    368
    BG Level
    4
    FFXI Server
    Carbuncle

    No real point to this, but...

    Code:
    #include <iostream>
    
    using namespace std;
    
    void main() {
    	int myGrade = 75;
    
    cout <<(char)( 'F' - ((min(max(myGrade,50),99)/10) - (myGrade<60 ? 5:4)))<<endl;
    
    }
    /twiddles

  10. #10
    Naver
    Guest

    That's some pretty optimized code. I'm curious how it works. Mind explaining what the min/max do?

  11. #11
    Banned.

    Join Date
    Oct 2006
    Posts
    10,115
    BG Level
    9

    Me me me.

    max(int, int) returns the bigger value.
    min(int, int) returns the smaller value.
    (something) ? [value]: [value] compact inline if.

    Example:

    myGrade = 73

    ( 'F' - ((min(max(myGrade,50),99)/10) - (myGrade<60 ? 5:4)))

    max(myGrade,50) = 73
    min(73,99) = 73
    73 /10 = 7
    myGrade <60?5:4 = 4 since 73 > 60.
    (7 - 4) = 3
    'F' - 3 = 67 which equals 'C'

  12. #12
    Very Sexy Nerd
    Join Date
    Oct 2005
    Posts
    8,732
    BG Level
    8
    FFXI Server
    Carbuncle

    Except that it's pretty damn unreadable and hard to understand.

    "Cool" sure I guess, but I wouldn't recommend actually using code like that for anything besides the novelty.

  13. #13
    Pay No Attention to the Man Behind the Curtain
    Join Date
    Jan 1970
    Posts
    3,481
    BG Level
    7
    FFXIV Character
    Ragns Meuhie
    FFXIV Server
    Gilgamesh
    FFXI Server
    Bahamut
    Blog Entries
    170

    In this specific case you could probably divide the grade by 10 to make it into a switch if that's what the teacher really want you to:

    Code:
    #include <iostream>
    
    using namespace std;
    
    void main() {
    	int myGrade = 75;
    	
    	switch ((int)Math.Floor(myGrade /10)) {
    		case 10:
    		case 9:
    			cout << "A" << endl;
    			system ("Pause");
    			break;
    		case 8:
    			cout << "B" << endl;
    			system ("Pause");
    			break;
    		case 7:
    			cout << "C" << endl;
    			system ("Pause");
    			break;
    		default:
    			cout << "F" << endl;
    			system ("Pause");
    	}
    }

  14. #14
    Banned.

    Join Date
    Oct 2006
    Posts
    10,115
    BG Level
    9

    Quote Originally Posted by Julian View Post
    Except that it's pretty damn unreadable and hard to understand.
    that is why // and /* */ are around.
    Now, which one causes less load (loool) to the CPU, now that you can argue.

    ITT: Working code that will get you an F (due to the teacher being picky)

  15. #15
    blax n gunz
    Join Date
    May 2005
    Posts
    11,141
    BG Level
    9

    Quote Originally Posted by Tajin View Post
    ITT: Working code that will get you an F (due to the teacher being picky)
    And on top of the teacher being picky, you'll fail exams if you never properly learned to construct control statements in your code.

  16. #16
    Murder machine with a motor in her nose
    Join Date
    Apr 2007
    Posts
    368
    BG Level
    4
    FFXI Server
    Carbuncle

    As I said, no real point to my example. Just saw the thread and tried to quickly figure out a way to do it within the cout statement in a fairly optimized way.

    the min/max is to cap the values at 5,6,7,8,9 since 1-4 and 10 won't work right. The second part is to shift 'E' grades down to 'F'.

    Ragns's code is essentially the same idea but a lot more readable. Only thing it needs is 'D' grades, a catch for grades > 109 and to move the pause out of the switch statement:

    Code:
    #include <iostream>
    
    using namespace std;
    
    void main() {
    	int myGrade = 75;
    	
    	switch ((int)Math.Floor(min(myGrade,100) /10)) {
    		case 10:
    		case 9:
    			cout << "A" << endl;
    			break;
    		case 8:
    			cout << "B" << endl;
    			break;
    		case 7:
    			cout << "C" << endl;
    			break;
    		case 6:
    			cout << "D" << endl;
    			break;
    		default:
    			cout << "F" << endl;
    	}
    	system ("Pause");
    }

  17. #17
    Cerberus
    Join Date
    Oct 2008
    Posts
    436
    BG Level
    4

    lol

    Lol thanks, my professor just wanted us to practice working on control statements. he wanted us to turn the if else statement into a switch statement. i just changed the if else statement to issue each grade a value then used the values for the switch. Thanks though.

  18. #18
    Relic Shield
    Join Date
    Jul 2008
    Posts
    1,951
    BG Level
    6
    FFXIV Character
    Audrey Weaver
    FFXIV Server
    Behemoth
    FFXI Server
    Asura

    Quote Originally Posted by Tajin View Post
    that is why // and /* */ are around.
    Now, which one causes less load (loool) to the CPU, now that you can argue.

    ITT: Working code that will get you an F (due to the teacher being picky)
    Actually the chain-of-if statements version might cause less load on the CPU. It depends on:
    - the grade distribution
    - how fast the min/max implementation is, compared to an if statement.

    For trivial implementations of min/max and uniform random distribution, they're actually probably going to be roughly the same thing.

    And yeah, there are faster ways to do this (as usual, if you want it to go faster, throw more memory at it). Just dividing it and using a switch statement on a compact range of values is also pretty good, those kinds of switch statements can usually be optimized by the compiler into a jump table. Incidentally, you would probably get bonus points if you could show you teacher you understand this, unlike the other solution.

  19. #19
    Sea Torques
    Join Date
    Jul 2006
    Posts
    500
    BG Level
    5
    FFXI Server
    Odin

    Quote Originally Posted by Kaelan? View Post
    Actually the chain-of-if statements version might cause less load on the CPU. It depends on:
    - the grade distribution
    - how fast the min/max implementation is, compared to an if statement.
    - your compiler.

    Compiling the same code with different compiler settings can already make a large difference in resulting code size or execution speed.

    Code:
    cout << myGrade>89?'A':myGrade>79?'B':myGrade>69?'C':myGrade>59?'D':'F' << endl;

  20. #20
    PROM KING OF
    SHOWDOWN HIGH

    Join Date
    Dec 2008
    Posts
    65
    BG Level
    2

    As the C++ FAQ Lite so eloquently puts it, Code must be written to be read, not by the compiler, but by another human being.

    Unless we're talking academics here, my suggestion would be to aim for the code that is first correct, and then easy to read.

    As the man Don Knuth himself said about optimization: "We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil."

    Or, as I myself would be inclined to say, "Nested ternary operators are for clever jerks."

Page 1 of 2 1 2 LastLast