What have I missed in this while-loop? [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 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' )

Related

Wrong type of input [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 have the code below, so basically my problem is: when someone input a character like 'a', it will pop a message that requires a re-input
I tried using the ASCII:
if (a >= 97 && a <= 122) but it still didn't work
double a;
cin >> a;
if (a >= 'a' && a <= 'z')
{
cout << "Wrong input, please re-input a: " << endl;
cin >> a;
}
cout << a;
I expect it to pop the message to re-input but the actual output is always 0 no matter what character I input
The state of a stream can be checked by using it directly in a condition. If all is okay it "returns" true, otherwise "false". So you can do e.g.
if (!(cin >> a))
{
// Invalid input, or other error
}
On invalid input you need to clear the state.
Note that if the input is invalid then the input will not be read, and the next time you attempt to read you will read the exact same input that failed the first time. One way to solve it is to ignore the rest of the line. Another is to read a whole line into a string that you then put into an input string stream for the parsing of the input.

Int output does not correspond given value [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 5 years ago.
Improve this question
My first stackoverflow post!
After entering a value for age into a declared and initialized int,
something weird happens and the value explodes. I test my code and could not see why it happens. After rechecking I can see that it is the last peice of code that did something to my int value.
I ask the stackoverflow gods "Why".
My code here:
int main()
{
cout << "Please enter your name and age\n\n";
string first_name;
int age(0);
cout << age << "\n\n"; // for testing why i get a huge number for age
cin>> first_name >> age;
cout << age << "\n\n"; // for testing why i get a huge number for age
cout << "Hello, " << first_name << " age " << age << '`\n';
keep_window_open(); // window must be closed manually
return 0;
}
This seems to be the offending bit:
'`\n';
This is the output I would get:
Please enter your name and age
0
et
23
23
Hello, et age 2324586
'`\n'
That's actually two characters, not only the newline feed. Plus you use single quotation marks, these are only used for single characters since char literals are of type const char.
The standard says:
The value of an integer character constant containing more than one
character (e.g., 'ab'), or containing a character or escape sequence
that does not map to a single-byte execution character, is
implementation-defined.
And thus the numbers after 23 : 24586 is the implementation-defined part that's causing weird output here. Use double quotes or '\n'.

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.

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 :)

How to find string that contain dash in a string [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 try to get the position of the string "-a" with this code
#include <iostream>
#include <string>
using namespace std;
int main(void)
{
string command("test –a string");
size_t pos = command.find("-a");
cout << "position found = " << pos << endl;
}
this produce this output:
position found = 4294967295
If I remove the '-' it's work as expected. 8 is returned.
You get the string::npos value returned, because the library thinks that it cannot find -a in the string.
The reason for this is that you use different dashes a long dash – in the string and a short dash - in the search string.
Once you replace the character with the correct one in both places, your code starts working fine (demo).
It means that there are different the first characters.
You can check this using the first characters and placing them in statement
std::cout << ( '–' == '-' ) << std::endl;
As they are different function find returns value std::string::npos that is defined as std::string::size_type( -1 ) or equal to 4294967295
If you look really close you will find the '–' is no '-'.