What's wrong with the cicle

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <cstdlib> 
#include <cmath>
#include <clocale>

using namespace std;

int main()
{
int j,s;
do{
	cin >> j;
	if (j == 1) { s = 10; }
	else if (j == 2) { s = 20; }
	else if (j == 3) { s = 30; }
	else cout << "Wrong button" << endl;
	} while (cin >> j && (j != '1' || j != '2' || j != '3'));
cout<<s;
	return 0;
}

Hi.
I want my program show 10,20,30 if I press 1,2,3 and if I press any button which are different from 1,2,3, it do a loop. But it dosent work(( What's wrong with my code?
closed account (E0p9LyTq)
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
#include <iostream>

int main()
{
   int j = 0;
   int s = 0;

   do
   {
      std::cin >> j;

      if (j == 1)
      {
         s = 10;
      }
      else if (j == 2)
      {
         s = 20;
      }
      else if (j == 3)
      {
         s = 30;
      }
      else
      {
         std::cout << "Wrong button\n";
      }
   } while (j < 1 || j > 3);

   std::cout << s << '\n';
}
5
Wrong button
7
Wrong button
3
30
Thank you for your reply. What if the user choose some wrong button, a letter, a,b,c,d any. That's the point of the circle in my main program. If I press some letter, something goes in a wrong way. In this case, how should I modify while circle?
while (cin >> j && (j != '1' || j != '2' || j != '3'));
You're checking if j is a char (single quotes ' are for chars, double quotes " are for string)

You defined j as an int:
int j = 0;

So the correct syntax in your case would be:
while (cin >> j && (j != 1 || j != 2 || j != 3));

However you should look at FurryGuy's code, his while statement is a lot more simple :)
Last edited on
closed account (E0p9LyTq)
What if the user choose some wrong button, a letter, a,b,c,d any.
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
#include <iostream>

int main()
{
   char j = ' ';
   int s  = 0;

   do
   {
      std::cin >> j;

      if (j == '1')
      {
         s = 10;
      }
      else if (j == '2')
      {
         s = 20;
      }
      else if (j == '3')
      {
         s = 30;
      }
      else
      {
         std::cout << "Wrong button\n";
      }
   } while (j != '1' && j != '2' && j != '3');

   std::cout << s << '\n';
}
b
Wrong button
5
Wrong button
3
30

Thank you. I have another question.
1
2
3
4
5
6
7
#include "stdafx.h"
#include <iostream>
#include <clocale>
int main() {
	std::cout << "\u0394V" << '\n';
	system("pause");
}
I have Windows 10 and Visual basic 2017. There is no eroor in the code.
But I get this "?V" instead of "ΔV". Is it possible to show greek symbols at console windows apps?
the default console supports extended ascii. EA has a small # of greek symbols but unfortunately, as far as I can tell, delta/triangle is not one of them. There may be a way to tell the console to accept Unicode, I do not know how. You can also go calc at it and call it dV maybe?
Last edited on
closed account (E0p9LyTq)
There is a warning when compiling the program:

warning C4566: character represented by universal-character-name '\u0394' cannot be represented in the current code page (1252)

You are trying to output a Unicode character with an ANSI stream.

https://stackoverflow.com/questions/32338496/what-is-the-difference-between-stdcout-and-stdwcout

Tweak the program to properly display Unicode:

1
2
3
4
5
6
#include <iostream>

int main()
{
   std::wcout << L"\u0394V\n";
}

And you get no visible output except for the new line!

Well, not actually true. The Unicode output is there, it just isn't visible with the standard CMD Windows shell.

https://www.curlybrace.com/words/2014/06/05/unicode-and-the-windows-console/
Topic archived. No new replies allowed.