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 '-'.
Related
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 1 year ago.
Improve this question
I've scoured but not finding a solution; this is part of a homework assignment so looking more for tips/explanation than outright solution.
Problem:
I am parsing a file and extracting key elements into a map. I've declared my standard non-const map as : map<label,element>. In a second phase of the program, I am needing to locate if exists in "map" and replace its value.
I'm able to find the element, and print it, but I can't seem to get it to change. It's not a constant, so it should be editable, but maybe I'm using the wrong function?
((For reference, i is a line number (19, current value stored in map), value_i is a stored int variable I'm trying to insert into my second element (current value is 0) ))
for (auto &el : labels) {
if (el.second == i) {
el.second == value_i;
std::cout << "Label " << el.first << " value changed to: " << el.second << std::endl;
}
Output:
Label n value changed to: 19
Desired Ouput:
Label n value changed to: 0
Thanks in advance!!
you made a simple mistake which is el.second == value_i; - you didn't assign value for second, you checked if its equal value_i. If your compiler didn't give you any warning about it, I recommend setting a higher level of warnings ( you can read online on how to do it on probably every compiler), that way you won't miss such small mistakes.
If you change this line of code to:
el.second = value_i;
It will do what you desired.
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' )
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'.
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
The output of the following code is 51 instead of the expected 5. I use Xcode 6.4 with the LLVM compiler on Mac OS Yosemite 10.10.4.
#include <iostream>
int main(int argc, const char * argv[]) {
std::cout << std::printf("%u", 5) << std::endl;
return 0;
}
If have tried to supply an int, unsigned long and uint32_t, and I have swapped the %u for a %d and %lu, but I always get the same result: A strange number being after the intended printf output. Swapping the 5 for another number results in another unexpected number being added to the output. This is in a fresh project with no other code added. What am I overlooking?
printf returns an int. In your case it is returning 1 which is the number of characters written (5).
cout is printing that 1, after printf prints 5 because you're chaining them together.
To just get 5, say:
std::printf("%u\n", 5)
printf returns number of printed characters, so '5' is printed by printf itself, then '1' is printed as result of cout << result_of_print
what you probably want is
std::cout << 5u << std::endl;
Also: avoid mixing C style output (printf) with C++ style (std::cout)
Your using both std::cout and std::printf.
Just one one of them, like this:
std::cout << 5 << std::endl;
OR
std::printf("%u", t);
You should not mix cout and printf together... what is happening is that printf returns the number of characters printed (in your case one character, which is 5). So the printf statement is executed, printing 5, but then cout prints the return value of printf, adding an extra 1.
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 :)