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'.
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 months ago.
This post was edited and submitted for review 3 months ago and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
I'm having problems with this simple example.
The program inquires as to how many letters are required to form a complete word. Then it will ask for each letter individually, which is fine, but I can't make the code save the value from the current character and the next one from the next iteration until the number of letters finishes the word and print it to confirm the word.
E.g. Let's say house, which has 5 letters.
int numbersOfCharacters=5;
int counter=0;
char character;
string phrase;
while (counter < numbersOfCharacters)
{
cout << "Introduce character's number" << counter << ": ";
cin >> character;
counter = counter + 1;
phrase=character+character; //I'm not sure if I need an array here.
}
cout << "Concatenated characters: " << phrase << endl;
The output is:
Introduce the character number 1: h
Introduce the character number 2: o
Introduce the character number 3: u
Introduce the character number 4: s
Introduce the character number 5: e
Concatenated characters: ?
And the expected output should be:
Concatenated characters: house
Update
Using John's comments I was able to resolve the issue. It's not a lack of debugging information but instead know the proper operator for this solution. Thanks also to Remy Lebeau for the detailed info.
The expression phrase=character+character; doesn't do what you think it does. You are taking the user's input, adding its numeric value to itself, and then assigning (not appending) that numeric result as a char to the string.
So, for example, on the 1st iteration, the letter h has an ASCII value of 104, which you double to 208, which is outside the ASCII range. On the next iteration, the letter o is ASCII 111 which you double to 222, which is also outside of ASCII. And so on. That is why the final string is not house like you are expecting.
Perhaps you meant to use phrase=phrase+character; instead? But, that won't work either, because you can't concatenate a char value directly to a string object using operator+.
What you can do is use string::operator+= instead:
phrase += character;
Or the string::push_back() method:
phrase.push_back(character);
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 needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
In order to substract a timestamp from my string i am using substring( pos,#char).
My code works partially. The problem that i have is consistency.
I am retrieving a string from a textfile in the format "Name Time" ,
Example : "Don 00:12".
this string is extracted from the file into HighScoreInfo1.
Also from a different file i am extracting a string, which is the name part, into previous_name.
With this information i am trying to calculate the position where the substring has to start.
int test = HighScoreInfo1.size() - (previous_name.size()+1 );
i already took measures for the extra char which is a space, thats the +2.
1 for the space and 1 to get to the position where he needs to start.
the timestamp, 00:00, is five characters long.
int test = HighScoreInfo1.size() - (previous_name.size()+2 );
TimeToBeat = HighScoreInfo1.substr(test, 5);
TestFile << "Orginal string size " << ">>" << HighScoreInfo1.size() << " " << "size of name"<< ">>" <<previous_name.size()+2<<" " << "Position "<< test << endl;
TestFile << "Orginal string " << ">>"<<HighScoreInfo1 << " " << "Substring"<< ">>" <<TimeToBeat << endl;
this is my output with a long name:
Orginal string size >>14 size of name>>10 Position >> 4
Orginal string >>Chrystel 00:09 Substring>>stel
Sidenote: you cant see it, but with the above example the space after "stel" is the fifth character.
This is my output with a short name:
Orginal string size >>9 size of name>>5 Position>> 4
Orginal string >>Don 00:09 Substring >>00:09
with the shorter name i get my timestamp perfectly.
How can make this works consistent.
I did not added the rest of my code because that part works. it is basically only the substring part that does not work consistent.
How can i fix this?
You're not calculating test correctly. It should just be 1 more than the size of the name, not the size of HighScoreInfo1 minus that (that's the length of the timestamp, which you know is 5). You only need to add 1, not 2, because positions are zero-based.
int test = previous_name.size() + 1;
DEMO
It only works with the shorter name by coincidence, because the name happens to be 3 characters long.
int test = HighScoreInfo1.size() - (previous_name.size()+2 );
This calculates the length of the time (which is always 5).
TimeToBeat = HighScoreInfo1.substr(test, 5);
This gets the characters 5-10. Always. Regardless of the length of the string. Perhaps you meant to do this, which reads the last five bytes?
TimeToBeat = HighScoreInfo1.substr(HighScoreInfo1.size()-5);
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 '-'.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
The program should be print the numbers 0 through 10, along with their values doubled and tripled. The data file contains the necessary variable declarations and output statements.
Example the output:
single 1 double 2 triple 3
single 2 double 4 triple 6
single 3 double 6 triple 9
single 4 double 8 triple 12
here my code tell me if correct
#include <iostream>
#include <cstdlib>
using namespace std;
int main ()
{
int x, double, triple;
int x = 0
while (x<=10)
{
cout << "x" << double = (x*2) << triple = (x*3) << endl;
x++;
}
return EXIT_SUCCESS
I'll attempt to put a guiding answer so I'm not going to give you a straight code that you can just copy and paste into your homework, but if you read and follow it, it should be the answer. (And next time, do go find your lecturer, or your tutor).
Some issues:
You are not printing the "Single" and "double" and "Triple" text which you should (based on your expected answer) So add that in.
You did your calculation to get the number for double, and triple - good. But again, you did not print them out.
Also C++ allows you to stack multiple cout all on one line, so for example:
cout << "My name is " << myname << endl;
Will print out:
My name is (content of variable myname)
And then print an end of line (endl). You can use that same technique to do part of your assignment to print out the results to meet the expected output.
Credit to Synetech
you miss a lot of code in there. You didn't print nothing what you want
try again with this inside while loop:
cout << “single “ << x << double << x*x
<<“ triple “<< x*x*x << endl;
x++;