Hello guys,
This code returns 2 errors and I dont understand why.

#include <iostream>
#include <string>
using namespace std;

char choice;
void M();
void C();
void Process();

void M(){
	cout <<"Multiplication\n";
}
void C(){
	cout << "Calculation\n";
}
void Process(){
	cout << "Make a choice\n"
		<< "C or M only\n";
	cin >> choice;
	while (strcmp(choice, "M")!=0) || (strcmp(choice, "C")!=0){
		cout << "ERROR Enter again";
	cin >> choice;
	
	}
	return;
}


void main () {
	Process();
	while (strcmp(choice, "M") == 0){
		M();
	}
	return;
	while (strcmp(choice, "C") == 0){
		C();
		return ;
	}
	
}

Any idea? thanks

Dani AI

Generated

A few focused corrections and a compact, modern-C++ example to make this thread more useful long after the original posts.

The original snippet (thanks to for posting it) had four problems worth highlighting: using a single char where a C-string comparison was attempted, a logic error that made the input loop never exit, unreachable code after return, and a non-standard void main. and were on the right track — here’s a clear, robust pattern that avoids those pitfalls and handles input safely.

#include <iostream>
#include <string>
#include <cctype>

static void showMultiplication() { std::cout << "Multiplication\n"; }
static void showCalculation()   { std::cout << "Calculation\n"; }

int main() {
    std::string line;
    char ch = 0;
    do {
        std::cout << "Make a choice (C or M only): ";
        if (!std::getline(std::cin, line)) return 1; // handle EOF/error
        if (line.empty()) continue;
        ch = static_cast<char>(std::toupper(static_cast<unsigned char>(line[0])));
        if (ch != 'M' && ch != 'C') std::cout << "ERROR Enter again\n";
    } while (ch != 'M' && ch != 'C');

    if (ch == 'M') showMultiplication(); else showCalculation();
    return 0;
}

Notes and quick tips:

  • Use std::string + std::getline to avoid leftover-newline issues from operator>>.
  • Validate with && when checking “not M and not C”; using || there makes the test always true.
  • If you must compare C-strings, include <cstring> and use strcmp(a,b) == 0. Prefer std::string in modern C++.
  • Cast to unsigned char before std::toupper to avoid undefined behavior on negative char values.
  • Keep main returning int, and avoid placing return statements which make later code unreachable.

Thanks to for the example and to , and for the discussion that identified the core issues.

Recommended Answers

All 5 Replies

Oh... I got it. I'm using strcmp to compare a char variable. :)

Good, well next time try doing the checking first Ok

Hello my poor c++ programmers
strcmp take two argument of type char * i.e array of character not char
i.e you can compare between
char *p = "SADDAM",l= "BOOSH";
cout << strcmp(p,l);
i hope you understand this thing

Hello my poor c++ programmers
strcmp take two argument of type char * i.e array of character not char
i.e you can compare between
char *p = "SADDAM",l= "BOOSH";
cout << strcmp(p,l);
i hope you understand this thing

What makes you so rich abu_sager?
:!:

Abu & FireNet,

Let's keep the discussion Technical, and leave the other stuff off the boards please. That is what we are here for. Let's choose our wording better next time.

Christian

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.