Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
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.
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.
Improve this question
I am trying to get only the alphabetic characters from an array of characters entered by the user. Here is a snippet:
const int SIZE(100);
int main()
{
char* entryTextArray = new char[SIZE];
char* adjustedTextArray= new char[SIZE];
int i, j;
cout << "Enter text, and I will tell you if it is a palindrome!" << endl;
cin.get(entryTextArray, SIZE);
cout << "Length of char array is " << strlen(entryTextArray) << endl;
for(i=0, j=0; i <= (strlen(entryTextArray)); i++) {
if(isalpha(entryTextArray[i]) && (entryTextArray[i] != '\0')) {
adjustedTextArray[j] = entryTextArray[i];
cout << adjustedTextArray[j] << endl;
j++;
}
}
cout << adjustedTextArray << endl;
}
When I compile, the cout of the adjustedTextArray displays the proper individual entrys, but the cout outside of the loop is the entry text, followed by garbage. I have no idea what is wrong! Help?!
You have the condition:
if (something && (entryTextArray[i] != '\0'))
so you are explicitly avoiding to copy the NUL terminating value from entryTextArray to adjustedTextArray. So you need to place it manually.
But since you are working in C++ using std::string just makes more sense.
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 2 months ago.
Improve this question
I'm currently getting into subroutines/subprograms or whatever you call them in english and in this specific assignment that i'm trying to solve I have to calculate the average length of two words.
void Length(string const text_1,
string const text_2,
int & total_length,
double & mean_length)
{
total_length = text_1.length() + text_2.length();
mean_length = static_cast<double>(total_length) / 2;
}
void Length_Program(int val)
{
string text_1;
string text_2;
int total_length{};
double mean_length{};
cout << "Mata in två ord: ";
cin >> text_1 >> text_2;
cout << "Totallängd: ";
Length(text_1, text_2, total_length, mean_length);
cout << total_length << endl;
cout << "Medellängd: " << fixed << setprecision(1) << mean_length;
}
I have set the precision to setprecision(1) and I assume it will only write one decimal but I keep getting two decimals.
my example is: abcd E
it should say that it is an average of 2.5 words but it says 2.51 for some reason. Can someone help me understand what i'm doing wrong?
Your problem is that you forgot << endl on your last output line. The return code shown by the OS is appended to your output. The setprecision is working just fine.
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 11 months ago.
The community reviewed whether to reopen this question 11 months ago and left it closed:
Original close reason(s) were not resolved
Improve this question
My "num = -num" line inside of my "if (num<0)" line still affects results, even if my input is greater than 0.
#include <iostream>
int main()
{
std::cout << "Enter a positive number: ";
int num{};
std::cin >> num;
if (num < 0)
std::cout << "Negative number entered. Making positive.\n";
num = -num;
std::cout << "You entered: " << num;
return 0;
}
To have multiple statements inside an if, you must use brackets.
And at this point of learning the language, I would recommend just always using brackets.
if (num < 0) {
std::cout << "Negative number entered. Making positive.\n";
num = -num;
}
Unlike languages like python, leading whitespace is not meaningful to the compiler in C++.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 5 years ago.
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.
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.
Improve this question
I've been reviewing my C++ lately. But I am running into a puzzle about printing a char array. The code is below:
int n = 5;
char *array1 = new char[n];
for (unsigned int i = 0; i < n - 1; i++)
array1[i] = (char)i;
cout << array1 << endl;
cout << array1[3] << endl;
cout << *array1 << endl;
None of the three cout lines works. Could anyone tell me why?
array1[0] == 0. cout << array1 interprets array1 as a pointer to a NUL-terminated string, and since the very first character is in fact NUL, the string is empty.
cout << array1[3] does print a character with ASCII code 3. It's a non-printable character, not visible to a naked eye. Not sure what output you expected to see there.
As a separate answer, it seems you're trying to get a string which has the following : array = "1234....(n-1)"
Try :
for (int i = 0; i<(n-1); i++)
array1[i] = (char)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 7 years ago.
Improve this question
I made a function that will reverse the string, but the output of the reversed string always shifts towards the right by one character.
#include <iostream>
#include <string>
using namespace std;
void reverse(string string1)
{
cout << endl;
for (int i = string1.size(); i >= 0; i--)
{
cout << string1[i];
}
cout << endl;
}
int main()
{
string string1;
getline(cin, string1);
reverse(string1);
system("pause");
return 0;
}
Your first output is of a character that does not exist.
std::string's leaky abstraction means that your first iteration is printing '\0', which apparently looks like a space in your configuration.
Begin at string1.size() - 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 have to write a function that goes through a string, finds any capital letters present and makes them lowercase. I decided to add a bit of code to show what letters were found and how many were found. Although on each compilation, the value of 'cnt' yields ridiculous results.
#include <iostream>
#include <vector>
using namespace std;
int upper(string s) {
int cnt;
vector<char> v{};
for (auto& i : s) {
if (isupper(i)) {
v.push_back(i);
i = tolower(i);
++cnt;
}
}
cout << "new string is '" << s << "'\n"
<< "number of capitals found is " << cnt << "\n"
<< "letters found were ";
for (auto l : v)
cout << l << " ";
return 0;
}
int main() {
string l = "This IS a TeSt";
upper(l);
}
I'm sure I must have done something wrong with the loop but whatever the problem is, I cannot find it.
The variable cnt is never initialized when used, change
int cnt;
to
int cnt = 0;
You failed to initialize the local variable cnt. Using an uninitialized value provokes undefined behavior, under which basically anything can happen.
Use int cnt=0; and please turn on all your compiler warnings.