#include <iostream>
using namespace std;
int main()
{
char sum[100],x[100];
while(x != '=' )
{
cout << "\n Enter number : ";
cin >> x;
sum=sum x;
}
cout << sum;
return 0;
}
having trouble creating a program where users can input as many numbers to sum up and will only end when they input '='.i cant seem to get this to work when i input the character '=' as the error, invalid operands of types 'char [100]' and 'char [100]' to binary 'operator ', pops up
CodePudding user response:
You may be mistaken with the type the variable x
is. x
is defined as a char array with the line char x[100]
so in order to see the values inside you need to specify an index such as x[49]
to get the 50th element which in your case would be the char value you wish to compare to "=".
A for loop may be useful as well to figuring out your problem.
CodePudding user response:
As the comment said, x is not a char but an array of char
I suggest using strcmp
function, if the value returned is different than 0 then they are different
Syntax :
int strcmp(const char* firstString, const char* secondString)
if(strcmp(x,"=")!= 0)
CodePudding user response:
If the goal is to check whether the user typed the single character '='
, all you need to do is look for it:
while (x[0] != '=')
// body of the loop
That works because x
is an array of char
; when you read into an array of char
the characters are inserted into the array in the order in which they are encountered. So if the user types just plain '='
that will be the first character in the array, i.e., x[0]
.
Note, however, that the array x
has not been initialized, so the first time through the loop, the value of x[0]
is indeterminate. That also has a simple fix:
char x[100] = "";
There's no reason to go to more complex solutions like strcmp
or std::string
for this part of the code.