Create a program that converts the PRICE CODE to PRICE VALUE.

Hello, I just wanted to help with my code
here's my questions

1. How can I input "computer" in any in any order and case insensitively and still get the correct output?

Here are the replacement
COMPUTERS.X
1234567890.X

2. If I input other letters that is not included in the COMPUTERS.X the program will terminate and ask again if i should input again.

example:

Input Code: Most.x
>UNABLE TO CONVERT YOUR INPUT


3. Is there any way to replace the whole string array instead of
``
replace(CODES.begin(),CODES.end(),'C','1');
``

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
#include <iostream>
#include <string.h>
#include <algorithm>

using namespace std;

int main()
{
    string CODES;
    char choice[5];
	
	do
	{
		cout << "Input code:  ";
		cin >> CODES;

	replace(CODES.begin(),CODES.end(),'C','1');
    	replace(CODES.begin(),CODES.end(),'O','2');
    	replace(CODES.begin(),CODES.end(),'M','3');
    	replace(CODES.begin(),CODES.end(),'P','4');
    	replace(CODES.begin(),CODES.end(),'U','5');
    	replace(CODES.begin(),CODES.end(),'T','6');
    	replace(CODES.begin(),CODES.end(),'E','7');
    	replace(CODES.begin(),CODES.end(),'R','8');
    	replace(CODES.begin(),CODES.end(),'S','9');
    	replace(CODES.begin(),CODES.end(),'X','0');
    	replace(CODES.begin(),CODES.end(),'c','1');
    	replace(CODES.begin(),CODES.end(),'o','2');
    	replace(CODES.begin(),CODES.end(),'m','3');
    	replace(CODES.begin(),CODES.end(),'p','4');
    	replace(CODES.begin(),CODES.end(),'u','5');
    	replace(CODES.begin(),CODES.end(),'t','6');
    	replace(CODES.begin(),CODES.end(),'e','7');
    	replace(CODES.begin(),CODES.end(),'r','8');
    	replace(CODES.begin(),CODES.end(),'s','9');
    	replace(CODES.begin(),CODES.end(),'x','0');
		
		cout << "Value: " << CODES << endl;
		
		cout << "Do you want to enter another code? (Y/N) ";
		cin >> choice;
	}
	while(strcmpi(choice, 'yes') == 0 ||strcmpi(choice, 'y') == 0 );
	{
        if (strcmpi(choice, 'no') == 0 || strcpmi(choice, 'no' == 0);
		{
        cout << "Program terminate";
		}
	}

	return 0;
}


any help will be appreciated, thank you very much
Last edited on
Maybe:

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
#include <iostream>
#include <cstring>
#include <cctype>

int main() {
	constexpr const char Codes[] {"computersxCOMPUTERSX"};
	constexpr const char Conv[] {"12345678901234567890"};
	unsigned char choice {};

	do {
		std::string code;

		std::cout << "Input code: ";
		std::cin >> code;

		for (auto& ch : code)
			if (const auto fnd {strchr(Codes, ch)}; fnd != nullptr)
				ch = Conv[fnd - Codes];
			else {
				std::cout << "Invalid input\n";
				code.clear();
				break;
			}

		if (!code.empty())
			std::cout << "Value: " << code << '\n';

		do {
			std::cout << "Do you want to enter another code? (Y/N) ";
			std::cin >> choice;
			choice = static_cast<unsigned char>(std::toupper(choice));

		} while (choice != 'N' && choice != 'Y' && (std::cout << "Invalid choice\n"));
	} while (choice =='Y');
}

 In function 'int main()':
17:42: error: expected ')' before ';' token
17:42: error: could not convert 'fnd' from 'const std::initializer_list<const char* const>' to 'bool'
17:19: warning: unused variable 'fnd' [-Wunused-variable]
17:44: error: 'fnd' was not declared in this scope
19:4: error: 'else' without a previous 'if'
7:23: warning: unused variable 'Conv' [-Wunused-variable]


i got this error
You need to compile for at least C++17. The latest standard is C++20.

What compiler/os are you using?
devc++ ISOc++11 windows 11
That code won't compile as C++11 - it needs at least C++17 as I mentioned above. If your compiler isn't at least C++17 compliant (and preferably c++20), then I suggest you update the compiler.

That code compiles OK with MS VS2022.

If that compiler can't be updated, then I suggest the free MS VS2022 community compiler https://visualstudio.microsoft.com/vs/

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
#include <iostream>
#include <string>
#include <cctype>
#include <map>
using namespace std;

int main()
{
   const string codesFrom = "COMPUTERS.X";
   const string codesTo   = "123456789.0";
   map<char,char> code;
   for ( int i = 0; i < codesFrom.size(); i++ )
   {
      char c = codesFrom[i], d = codesTo[i];
      code[tolower( c )] = code[toupper( c )] = d;
   }
   
   for ( char ans = 'y'; tolower( ans ) == 'y'; cin >> ans )
   {
      string word, drow;
      cout << "Enter word to be encoded: ";   cin >> word;
      bool ok = true;
      for ( char c : word )
      {
         auto it = code.find( c );
         if ( it == code.end() )
         {
            ok = false;
            break;
         }
         drow += it->second;
      }
      
      if ( ok )
      {
          cout << "Encoded: " << drow << '\n';
          break;
      }
      else
      {
          cout << "Unencodeable. Try again (y/n)? "; 
      }
   }
}


Enter word to be encoded: cow
Unencodeable. Try again (y/n)? y
Enter word to be encoded: cORe
Encoded: 1287



BTW The title of your post ... seems to have little bearing on what you are trying to do!
Last edited on
whitegentle1 wrote:
devc++ ISOc++11 windows 11

Even in the recent Embarcadero and Red Panda incarnations of the Integrated Development Environment (IDE) you won't be able to use any C++ standard later than C++11 even if the underlying compiler is capable of the later standard.

There are a couple of free C++ IDEs that are years ahead of Dev-C++: Visual Studio 2022 and Code::Blocks. VS uses MSVC++ compiler, C:B is available bundled with GCC/MinGW though it can use other compilers.

I have both, my personal preference is VS. Currently it is fully C++20 compliant, no other compiler is 100%.

Since you are using Windows 11 using Visual Studio would be a good bet. You'll have to do a custom install, C++ is not a default package.
you can use a map, but just using basic C constructs. Half the code is just a constant table.
the other half is simple and will work with C++ 1990s compilers if you trade out the string & RBFL. If you need it degraded back that far?

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
int main ()
{
 char tbl[256]{};
 tbl['c'] = tbl['C']='1'; 
 tbl['o'] = tbl['O']='2';
 tbl['m'] = tbl['M']='3';
 tbl['p'] = tbl['P']='4';
 tbl['u'] = tbl['U']='5';
 tbl['t'] = tbl['T']='6';
 tbl['e'] = tbl['E']='7';
 tbl['r'] = tbl['R']='8';
 tbl['s'] = tbl['S']='9'; 
 tbl['x'] = tbl['X']='0';
 tbl['.']='.';
 
 bool bad{false};
 do
 {
	 string in,out;
	 bad = false;
	 cout << "enter word\n";
	 cin >> in; 
	 for(char c:in)
	 {
		if(tbl[c])
		 out += tbl[c];
		else bad = true;
	 }
	if(!bad) cout << out << endl;
 }while(bad);
}
Last edited on
Thank you very much for your help, I found the solution for my prob. I've derived many codes to get fitted to my likes and downloaded visual studio for better compiling ooptions, im sorry im just a newbie
Here's some lessons on using Visual Studio to make transitioning easier.

https://www.learncpp.com/cpp-tutorial/installing-an-integrated-development-environment-ide/

The pictures show VS 2019, the process with VS 2022 is the same.

I recommend reading all of Learn C++'s chapter 0, and as you get comfortable with VS dive into the other lessons.
Topic archived. No new replies allowed.