M
M
Meldas232020-04-14 18:10:30
C++ / C#
Meldas23, 2020-04-14 18:10:30

Compiler outputs "�", why?

If you select action "1" - everything works, but if you select 2-5, then the compiler issues this character "�"

#include <iostream>
#include <string> 
double power(double n, int p);
char power(char n, int p);
int power(int n, int p);
long power(long n, int p);
float power(float n, int p);
using namespace std;
enum Chose{int_, double_, float_, long_, char_};


int main()
{
  Chose chose;
  int chose_;
  cout << "Выберите действие: ";
  cin >> chose_;
  
  switch(chose_)
  {
    case 1:
      chose = int_;
      break;
    case 2:
      chose = double_;
    case 3:
      chose = float_;
    case 4:
      chose = long_;
    case 5:
      chose = char_;
  
  }
  
  
  switch(chose)
  {
    case int_:
    {		
        int power_;
        int number;
        cout << "Введите число: ";
        cin >> number;
        cout << "Введите степень: ";
        cin >> power_;
        int a = power(number, power_);
        cout << a;
        
        break;
    }
    case double_:
    {		
        double nu;
        int po;
        cout << "Введите число: ";
        cin >> nu;
        cout << "Введите степень: ";
        cin >> po;
  
        double a = power(nu, po);
        cout << a;
        
        break;
    }
    case float_:
    {		
        int power_;
        float number;
        cout << "Введите число: ";
        cin >> number;
        cout << "Введите степень: ";
        cin >> power_;
        float a = power(number, power_);
        cout << a;
        
        break;
    }
    case char_:
    {
        int power_;
        char number;
        cout << "Введите число: ";
        cin >> number;
        cout << "Введите степень: ";
        cin >> power_;
        char a = power(number, power_);
        cout << a;
        
        break;
    }
    case long_:
    {
        int power_;
        long number;
        cout << "Введите число: ";
        cin >> number;
        cout << "Введите степень: ";
        cin >> power_;
        char a = power(number, power_);
        cout << a;
        
        break;
    }
  
  
  
  
  }
    
    
}
//////////////////
int power(int n, int p)
{
  int result = 1;
  for(int i = 0; i < p; i++)
  {
    result = result * n;
  }
  return result;
}
//////////////////
long power(long n, int p)
{
  long result = 1;
  for(int i = 0; i < p; i++)
  {
    result *= n;
  }
  return result;
}
//////////////////
double power(double n, int p)
{
  double result = 1;
  for(int i = 0; i < p; i++)
  {
    result *= n;
  }
  return result;
}
//////////////////
char power(char n, int p)
{
  char result = 1;
  for(int i = 0; i < p; i++)
  {
    result *= n;
  }
  return result;
}
//////////////////
float power(float n, int p)
{
  float result = 1;
  for(int i = 0; i < p; i++)
  {
    result *= n;
  }
  return result;
}
//////////////////

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
edward_freedom, 2020-04-14
@Meldas23

Because, when choosing 1, your condition falls into int_, in any other subsequent one in char_, at least you write 2, at least 4 write, it will fall into 5, because it is the last one. And all because you correctly put break only on the first condition, but not on the rest and there will always be the last condition.

case 1:
    chose = int_;
    break;
  case 2:
    chose = double_;
    break;
  case 3:
    chose = float_;
    break;
  case 4:
    chose = long_;
    break;
  case 5:
    chose = char_;
    break;

Your code can be shortened with templates
#include <iostream>
#include <string> 
using namespace std;
enum Chose { int_, double_, float_, long_, char_ };


template<typename Tn, typename Tp>
Tn Power(Tn n, Tp p) {
  Tn  result =  1;
  for (int i = 0; i < p; i++)
  {
    result = result * n;
  }
  return result;
}

template<typename Tn, typename Tp>
void UserInput() {
  Tn  power =  1;
  Tp  number =  1;
  cout << "Введите число: ";
  cin >> number;
  cout << "Введите степень: ";
  cin >> power;
  Tn result = Power(number, power);
  cout << "Result: " << result << endl;
}


int main()
{
  setlocale(LC_ALL, "russian");
  Chose chose;
  int chose_;
  cout << "Выберите действие: ";
  cin >> chose_;

  switch (chose_)
  {
  case 1:
    UserInput<int, int>();
    break;
  case 2:
    UserInput<double, int>();
    break;
  case 3:
    UserInput<int, float>();
    break;
  case 4:
    UserInput<int, long>();
    break;
  case 5:
    UserInput<double, char>();
    break;
  }

  system("pause");
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question