Switch statment

I have a switch statement where it counts how many times I input the number 1. However I just want it to store the increment the value when i put one digit which is 1, because when I put a number like 521134 it will still count the two 1 in the number 521134. How can I make it count the number 1 as in one digit only. If this is unclear I can post the code.
Sounds to me like you are inputting as a string and then looping?

if so maybe something like
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int main()
{
    std::string input("");
    std::cout << "number: " << std::endl;
    std::cin >> input;
    int count( 0 ) , temp_count( 0 );
    for( const auto &it : input )
   {
        switch( it )
        {
            case '1': ++temp_count; break; //I hope you have more in switch than 1..
        }
        if( temp_count > count )
        {
            ++count;
            break;
         }
    }
}
hmm Im not sure here is my code.

// in my header file i have

class Manager
{
private:
double salary;

public:

void setSalary();
double getSalary();
void calcSalary();
void displayMessage();


};




//In my first cpp file i have
include<iostream>
#include"Manager.h"
#include<string>



void Manager::setSalary()
{
double Salary;
std::cout<< "Enter weekly salary: ";
std::cin>>Salary;

salary = Salary;

}


void Manager::calcSalary()
{

salary=salary;


}

double Manager::getSalary()
{

return salary;

}

void Manager::displayMessage()
{
std::cout<<"Manager's pay is: $"<<getSalary()<<std::endl;

}



//in my main fuction file, and there where the switch statement is i have:


#include <iostream>
#include"Manager.h"
#include<iomanip>

void mechanism()
{
int code;
Manager object;
std::cout<<"Enter paycode or -1 to end: ";
std::cout<<std::setprecision(2)<<std::fixed;



while( (code=std::cin.get())!= -1)
{//start while loop


switch(code)
{

case '1':
std::cout<<"Manager Selected"<<std::endl;
object.setSalary();
object.calcSalary();
object.displayMessage();
break;

case'\n':
case'\t':
case' ':
break;

default:
std::cout<<"Wrong number entered"<<std::endl;
break;


}

}// end while loop

}//end function

int main()
{


mechanism();


}





Thank you a lot for your help . I hope u can solve this
I still don't understand your question or your class.. What is calc salary and get salary supposed to be doing? Also AFAIK std::get is for one character only so might be hard to do -1 that is two characters. You could use >> and that will get input into a white space which would allow -1. Also on your set salary function the second variable is pointless imo. You can just do
1
2
3
4
5
void set_salary()
{
    std::cout << "Please enter salary: " << std::flush;
    std::cin >> salaray; //assigns value to the private in class
}


Then for your getmessage function you can just output the private variable.
1
2
3
4
void get_message()
{
    std::cout << "I am a message with a salary of " << salary << std::endl;
}


*edit also please use code tags <> in the format section or type
[code][/code]
around your code.
Your switch should also be case 1: not case '1': since code is an int not a char. If you wanted to use a char as a case you could do case '1' - '0': I suppose though.
Last edited on
Yeah sorry didn't know how to do that. until now. I need calc salary just in case im gonna do a calculation. I won't use it though. The get salary is to return the value of salary. Because I learned that when u set a variable in private you need to get it my making getters and setters functions.
I fixed the the switch while loop and still it is not working.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60

include<string>

void mechanism()
{
    int code;
    Manager object;
    std::cout<<std::setprecision(2)<<std::fixed;

    std::cout<<"Enter paycode or -1 to end:";
    std::cin>>code;

  



    while( code!= -1) 
{//start while loop
    
   
    switch(code)
    {
   
    case '1':
        std::cout<<"Manager Selected"<<std::endl;
        object.setSalary();
        object.calcSalary();
        object.displayMessage();
        break;

    case'\n':
    case'\t':
    case' ':
        break;
    
    default:
        std::cout<<"Wrong number entered"<<std::endl;
        break;
    
    
     }
     std::cout<<"Enter paycode or -1 to end: ";
     std::cin>>code;


   }// end while loop

 }//end function

int main()
{


mechanism();


}


you still have case '1': instead of case 1:........Also you have two "getters" and btw getters/setter's aren't very good you should ask for input before creating the object then use an initializer list. ex:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
class Object
{
public:
    Object( void ); //default
    Object( const short& ) //passing parameter
    friend std::ofstream& operator<< ( std::ofstream& , const Object& );
private:
    short salary; //you can make const if you are not going to be modifying.
}

Object::Object( void ) : salary( 10 ) {} // default salary is 10

Object::Object( const short &value ) : value( value ){} //initailize private value to parameter value

std::ofstream& operator<< ( std::ofstream &stm , const Object &obj )
{
    return( stm << obj.salary ); //friend has access to private.
}

int main()
{
    short input( 0 );
    std::cout << "Input: " << std::flush;
    std::cin >> input;
    Object obj1 /*default*/ , obj2( input ) /*parameter*/;
    std::cout << obj1 << std::endl << obj2 << std::endl;
}
The problem was I had case '1': instead of case 1: you are right. I also changed the two getters I had and made one. So much more organized now. I thought to get something you should always return it, that's why I had it.
Thanks a lot that made everything much better.
Topic archived. No new replies allowed.