Is it possible in C++ to type multiple lines of string outputs without have to continually add the prefix "cout"? [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 2 years ago.
Improve this question
I just started learning C++. I have seen examples of coders typing code like this...
int main() {
cout << "hello";
<< "world";
return 0;
}
but when I tried that it seemed to me that I have to write it like this...
int main() {
cout << "hello";
cout << "world";
return 0;
}
How can I do this like the original example shows?

You might mean:
int main() {
cout << "hello" // <- no trailing `;`
<< "world";
return 0;
}
That will print:
helloworld
If you wanted then on separated rows, you can write:
int main() {
cout << "hello\n"
<< "world\n";
return 0;
}
That will produce:
hello
world
But, there is another, less known, feature of the compiler - concatenation
of raw strings:
int main() {
cout << "hello"
"world";
return 0;
}
That will also work. This too gives helloworld as output.

Related

The output begins with space [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
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.

C++ char arrays -- Why the garbage? [closed]

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.

why am I getting random results when incrementing an int [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 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.

Can't Print String Array Element [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
Whenever I try to run this program it returns an error saying:
no operator "<<" matches these operands
Also note that the program only runs into this problem in the getChoice() function.
#include <iostream>
#include "utilities.h"
using namespace std;
int getChoice(string inChoices[]){
int numOfChoices = sizeof(inChoices) / sizeof(inChoices[0]);
string x = inChoices[0];
string y = inChoices[1];
cout << x << endl << y << endl;
return numOfChoices;
}
int main()
{
string choices[2] = { "Happy Day", "Even Better Day" };
cout << utilities::getChoice(choices) << endl;
cout << endl << sizeof(choices) / sizeof(choices[0]) << endl;
}
You need also to include the string header:
#include <string>
You need to #include <string>
And your calculation of numOfChoices in getChoice() is wrong, since the parameter inChoices is actually a "pointer to string" instead of "array of strings".

C++ Xcode lldb error: loops never work [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 9 years ago.
Improve this question
thanks for taking the time to help me out.
I'm really new with C++ and Xcode. I was working on a simple program to help me understand loops, so my goal was to make a simple "echo machine". This is my code:
string words;
int main()
{
do {
cout << "Enter text.";
cin >> words;
cout << "You entetered " << words << "!";
}
while (words != "goodbye");
return 0;
}
My result is nothing but lldb in parenthesis. I am very frustrated and can't find what I'm doing wrong anywhere. Please help and thank you so much.
Are you just missing the include directives for the standard headers you're using?
Try this:
#include <iostream>
#include <string>
using namespace std;
int main() {
string words;
do {
cout << "Enter text: ";
cin >> words;
cout << "You entered " << words << "!\n";
} while (words != "goodbye");
return 0;
}