C++ comparing a character of a string [closed] - c++

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 years ago.
Improve this question
Ok, so input is a string. When I try to compile the following code I get
c.cpp:42:10: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
Why?
if(input[i] != ' ')
{
char s = input[i];
if(s == "+")
{
...
}
}

Use single quotes in this statement
if(s == "+")
as here
if(s == '+')

As char s is a character so it can only be compared against another character or ascii value.
Double quotes (" ") are used for string while single quotes (' ') for characters.

Related

What have I missed in this while-loop? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I am trying to write a simple program that reads a name as a C-style string.
The name is then printed vertically, one character per line.
Currently when the program prompts a user to enter their name, eg. Henry James, only 'Henry' is printed vertically. It stops printing at the break between the names.
char myName[ 64 ] = "";
cout << "Enter your name: ";
cin.get( myName, 64 );
int i = 0;
while ( myName [ i ] != ' ' )
{
cout << myName[ i ] << endl;
i++;
}
getch();
return 0;
I've tried putting cin.ignore() the line before cin.get(), but this ends up breaking the program. What am I missing in the while loop?
You explicitly write that your loop should stop at ' ' space character. Everything as expected :-)
If you want to print until end of the C style string, check against the terminating char which is a zero.
while ( myName [ i ] != '\0' )

C++ Loops forever [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I wrote the following loop as a part of a program for my CS homework, however regardless of the input, the program keeps looping at this exact point. What am I doing wrong?
#include <iostream>
using namespace std;
char choice;
do
{
cout << "Type 'c' for characters or type 'n' for numbers: ";
cin >> choice;
}while (choice != 'c' || choice != 'n');
A do-while statement loops as long as the while expression is true.
Your while expression is
choice != 'c' || choice != 'n'
In common English, that expression means
choice is not 'c' OR choice is not 'n'
That statement, logically, is always true. choice is always not one of those things.
In both English and C++, you would want to use and/&& in that expression.

The array index doesnt show the character [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I have this code:
#include <stdio.h>
int main(){
char s1[30] = "This is a sentence";
for(int i = 0; i<sizeof(s1);i++){
if(s1[i] = ' '){
printf("+");
}
}
return 0;
}
When I try to loop the array to find all the spaces this happens:
Output: ++++++++++++++++++++++++++++ //30 pluses.
Why doesnt my program outputs 3 pluses?
EDIT: My problem was a simply typo mistake, If you didn't understand what is wrong here take a look at accepted answer.
Change = to == in your if statement.
In your conditional statement, you're assigning space to s[ i ] (operator =). You want to compare them (operator ==).
Try
if (s[ i ] == ' ')
s[ i ] = ' ' is always true because the result of an assignment is the value assigned (space). This value is implicitly converted to a bool (0 = false, anything else = true). Since a space is 32 in ASCII, it will always be true.
References - Assignment Operator, Comparison Operators, ASCII Table
Do this:
if(s1[i] == ' '){
printf("+");
}
= is an assignment operator. To compare two value you need to use == operator. You have used = that that assignment operator always return true so + is being printed out all the time.

save variable from if statement [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
Couldn't find an answer on google because I didn't know how to phrase is.
I have a regular function as below and would like to update the variable number in the first if statement. I've tried all sorts of combos but nothing works.
int main()
{
int apple, number;
cout << "Enter you number"<< endl;
cin >> apple;
if (apple == 1){
number = 2;
}
else {
number = 3;
cout << number << endl;
}
How would I change the above so I get 2 to output to the screen?
Thanks in advance!
You need to use
if (apple == 1)
instead of
if (apple = 1)
== is used for comparison. Also to note that your code will always assign the value 2 to the variable apple as in your condition you are not comparing rather you are assigning. So in your case the output will always be 2.

How to display Degree Celsius in a string in C++ [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I need to display a string with values like 36 Deg Celsius.
string sFinish = NULL;
string sValue = "36";
sFinish.append(sValue);
sFinish.append(" Deg Celsuis");
cout<<"Degree = "<<sFinish;
I am not able to figure out how to display degree (o symbol) instead of writing "Deg Celsius".
If you just copy paste "°" string into code - it shows extra character - like this "°".
Try:
std::cout << "Temperature: " << sValue << "\370";
You might find the following link helpful for the full ascii table.
Here is a solution I found here on SO: Including decimal equivalent of a char in a character array
But to summarize, this would do fine
char * val = "37";
string temp(val);
temp.append("\xB0");
cout << temp;
Just in-case if anyone wants to try this:
sFinish.append("\u2103");
this will display Deg celsius :)