many errors when using while loop

hello, i'm a beginner and i'm writing a program in netbeans that can add togather 2,3 and 4 numbers.then i thought of using do-while loop to prompt user to continue or exit but its giving errors.
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
int a,b,k,l,m,n,p,q,r;
int add2(int a, int b) {
    cout<<"Enter first number:";
    cin>>a;
    cout<<"Enter second number:";
    cin >> b;
    cout<<"Sum of "<<a<<"and"<<b<<"is"<<a+b<<"."<<endl;
    return 0;
}
int add3(int p, int q, int r) {
    cout<<"Enter first number:";
    cin>>p;
    cout<<"Enter second number:";
    cin >> q;
    cout<<"Enter third number:";
    cin>>r;
    cout<<"Sum of "<<p<<", "<<q<<", "<<r<<" is "<<p+q+r<<endl;
    return 0;
}
int add4(int k, int l, int m, int n) {
    cout<<"Enter first number:";
    cin>>k;
    cout<<"Enter second number:";
    cin >> l;
    cout<<"Enter third number:";
    cin>>m;
    cout<<"Enter fourth number:";
    cin>>n;
    cout<<"Sum of "<<k<<", "<<l<<", "<<m<<" and "<<n<<" is "<<k + l + m + n<<endl;
    return 0;
}
int main() {
    string choice;
    do {
    int en;
   cout<<"How many numbers you want to add (max 4) :";
   cin>>en;
   if (en==2) {
    add2 (a, b);
   }
   else if(en==3) {
    add3(p, q, r);
   }
   else if (en==4) {
    add4(k, l, m, n);
   }
   else {
    cout<<"wrong input"<<endl;
   }
   cout<<"Do you want to continue:";
   cin>>choice;
  }
   while (choice == 'yes'); 
}

when i compile the file it gives following errors.(im unable to under stand what are errors so im posting it all)

basicadd.cpp:55:21: warning: multi-character character constant [-Wmultichar]
while (choice == 'yes');
basicadd.cpp: In function 'int main()':
basicadd.cpp:55:18: error: no match for 'operator==' (operand types are 'std::string {aka std::basic_string<char>}' and 'int')
while (choice == 'yes');
c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\postypes.h:216:5: note: template<class _StateT> bool std::operator==(const std::fpos<_StateT>&, const std::fpos<_StateT>&)
operator==(const fpos<_StateT>& __lhs, const fpos<_StateT>& __rhs)
c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\postypes.h:216:5: note: template argument deduction/substitution failed:
basicadd.cpp:55:21: note: 'std::string {aka std::basic_string<char>}' is not derived from 'const std::fpos<_StateT>'
while (choice == 'yes');
c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\stl_pair.h:214:5: note: template<class _T1, class _T2> bool std::operator==(const std::pair<_T1, _T2>&, const std::pair<_T1, _T2>&)
operator==(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y)
c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\stl_pair.h:214:5: note: template argument deduction/substitution failed:
basicadd.cpp:55:21: note: 'std::string {aka std::basic_string<char>}' is not derived from 'const std::pair<_T1, _T2>'
while (choice == 'yes');
c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\stl_iterator.h:291:5: note: template<class _Iterator> bool std::operator==(const std::reverse_iterator<_Iterator>&, const std::reverse_iterator<_Iterator>&)
c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\stl_iterator.h:291:5: note: template argument deduction/substitution failed:
basicadd.cpp:55:21: note: 'std::string {aka std::basic_string<char>}' is not derived from 'const std::reverse_iterator<_Iterator>'
while (choice == 'yes');
c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\stl_iterator.h:341:5: note: template<class _IteratorL, class _IteratorR> bool std::operator==(const std::reverse_iterator<_Iterator>&, const std::reverse_iterator<_IteratorR>&)
operator==(const reverse_iterator<_IteratorL>& __x,
c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\stl_iterator.h:341:5: note: template argument deduction/substitution failed:
basicadd.cpp:55:21: note: 'std::string {aka std::basic_string<char>}' is not derived from 'const std::reverse_iterator<_Iterator>'
while (choice == 'yes');
c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\allocator.h:128:5: note: template<class _T1, class _T2> bool std::operator==(const std::allocator<_CharT>&, const std::allocator<_T2>&)
operator==(const allocator<_T1>&, const allocator<_T2>&)
c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\allocator.h:128:5: note: template argument deduction/substitution failed:
basicadd.cpp:55:21: note: 'std::string {aka std::basic_string<char>}' is not derived from 'const std::allocator<_CharT>'
while (choice == 'yes');
c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\allocator.h:133:5: note: template<class _Tp> bool std::operator==(const std::allocator<_CharT>&, const std::allocator<_CharT>&)
operator==(const allocator<_Tp>&, const allocator<_Tp>&)
c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\allocator.h:133:5: note: template argument deduction/substitution failed:
basicadd.cpp:55:21: note: 'std::string {aka std::basic_string<char>}' is not derived from 'const std::allocator<_CharT>'
while (choice == 'yes');
operator==(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\basic_string.h:2486:5: note: template argument deduction/substitution failed:
basicadd.cpp:55:21: note: mismatched types 'const std::basic_string<_CharT, _Traits, _Alloc>' and 'int'
while (choice == 'yes');
c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\basic_string.h:2493:5: note: template<class _CharT> typename __gnu_cxx::__enable_if<std::__is_char<_Tp>::__value, bool>::__type std::operator==(const std::basic_string<_CharT>&, const std::basic_string<_CharT>&)
operator==(const basic_string<_CharT>& __lhs,
c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\basic_string.h:2493:5: note: template argument deduction/substitution failed:
basicadd.cpp:55:21: note: mismatched types 'const std::basic_string<_CharT>' and 'int'
while (choice == 'yes');
c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\basic_string.h:2507:5: note: template<class _CharT, class _Traits, class _Alloc> bool std::operator==(const _CharT*, const std::basic_string<_CharT, _Traits, _Alloc>&)
operator==(const _CharT* __lhs,
c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\basic_string.h:2507:5: note: template argument deduction/substitution failed:
basicadd.cpp:55:21: note: mismatched types 'const _CharT*' and 'std::basic_string<char>'
while (choice == 'yes');
c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\basic_string.h:2519:5: note: template<class _CharT, class _Traits, class _Alloc> bool std::operator==(const std::basic_string<_CharT, _Traits, _Alloc>&, const _CharT*)
operator==(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\basic_string.h:2519:5: note: template argument deduction/substitution failed:
basicadd.cpp:55:21: note: mismatched types 'const _CharT*' and 'int'
while (choice == 'yes');
c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\streambuf_iterator.h:204:5: note: template<class _CharT, class _Traits> bool std::operator==(const std::istreambuf_iterator<_CharT, _Traits>&, const std::istreambuf_iterator<_CharT, _Traits>&)
operator==(const istreambuf_iterator<_CharT, _Traits>& __a,

c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\streambuf_iterator.h:204:5: note: template argument deduction/substitution failed:
basicadd.cpp:55:21: note: 'std::string {aka std::basic_string<char>}' is not derived from 'const std::istreambuf_iterator<_CharT, _Traits>'
while (choice == 'yes');
c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\ext\new_allocator.h:139:5: note: template<class _Tp> bool __gnu_cxx::operator==(const __gnu_cxx::new_allocator<_Tp>&, const __gnu_cxx::new_allocator<_Tp>&)
operator==(const new_allocator<_Tp>&, const new_allocator<_Tp>&
from __gnu_cxx::__normal_iterator<_Iterator, _Container>&)
operator==(const __normal_iterator<_Iterator, _Container>& __lhs,
while (choice == 'yes');
sorry for my bad english.
any help will be appreciated.thanks
That's a nice litany you got there :)
First, you don't need these global variables at the beggining:
 
int a,b,k,l,m,n,p,q,r;

Secondly, you don't need to use arguments.

Also, let's take look at your function:
1
2
3
4
5
6
7
8
int add2(int a, int b) {
    cout<<"Enter first number:";
    cin>>a;
    cout<<"Enter second number:";
    cin >> b;
    cout<<"Sum of "<<a<<"and"<<b<<"is"<<a+b<<"."<<endl;
    return 0;
}

What is the point of making function return an int, when it's always 0? Better make it void.

1
2
3
4
5
6
7
8
9
void add2()
{
    int a,b;
    cout<<"Enter first number:";
    cin>>a;
    cout<<"Enter second number:";
    cin >> b;
    cout<<"Sum of "<<a<<"and"<<b<<"is"<<a+b<<"."<<endl;
}


With this function, you would simply do
1
2
3
if (en==2) {
    add2();
   }


Much more simple.

Also, mind the difference between ' and ".(line 53)

' is for single character(char): 'a', '*', etc.

" is for string(std::string, char*, char[]) (the difference is that string will always have null character('\0') at the end. e.g. "Hello!", "lol", but it can also be "a"("a" will literally be char[2] consisting of 'a' and '\0').

Last edited on
thank you so much MatthewRock.:) this helped me a lot.
Topic archived. No new replies allowed.