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 7 years ago.
Improve this question
So, I started using C++ today, and wanted to make a small, text-based adventure, but that didn't turn out all that well.
Here's my code:
string answer;
string name;
string charName;
std::cout << "<Ominous voice> Hi there, what's your name?" << std::endl;
std::cout << "Enter your name:" << std::endl;
getline(cin, name);
std::cout << "<Ominous voice> " << name << "? That's an... interesting name." << std::endl;
std::cin.ignore();
std::cout << "<Ominous voice> Before we start off... You need to learn the basics, so let's break the fourth wall shall we?" << std::endl;
std::cin.ignore();
std::cout << "<Ominous voice> I'm here to guide you on this wonderfull adventure... for just $ 9.99." << std::endl;
std::cin.ignore();
std::cout << "<Ominous voice> You can name me everything you want, since I'm a fragment of your imagination anyway... So, what about it?" << std::endl;
std::cout << "Enter a name:" << std::endl;
getline(cin, charName);
strcpy (str1,"<");
strcpy (str2,">");
strcat (str1,charName, 1);
strcat (charName,str2, charName.size();
std::cout << charName << " Well then, it seems I'm now called '" << charName << "' not sure if I like that." << std::endl;
std::cin.ignore();
std::cout << "This line does not show up" << std::endl;
Now I have 2 problems:
I get these messages when I start the program:
/home/ubuntu/workspace/hello-cpp-world.cc: In function ‘int main()’:
/home/ubuntu/workspace/hello-cpp-world.cc:23:13: error: ‘str1’ was not declared in this scope
strcpy (str1,"<");
^
/home/ubuntu/workspace/hello-cpp-world.cc:24:13: error: ‘str2’ was not declared in this scope
strcpy (str2,">");
^
/home/ubuntu/workspace/hello-cpp-world.cc:26:43: error: expected ‘)’ before ‘;’ token
strcat (charName,str2, charName.size();
^
That last line, saying "This line does not show up", actually doesn't show up.
I know I'm basic and this is probably not the most efficient way to do this, but I'm a beginner.
You have to declare string str1 before using it. Decraling means
std::string str1;
In C++ the definition is e.g.
str1 = "heureka";
And the declaration has always be in front of the definition.
But you can do both in one line:
std::string str1 = "heureka";
or
std::string str1("heureka");
If you want to add "<" in front and ">" behind the characters name. You can do it as follows:
charName = "<" + charName + ">";
There is no need to create new strings for it. There is simply a + operator for strings and it is easier to use than strcpy, strcat or so.
The issue is that you haven't declared the strings str1 and str2.
Related
This question already has answers here:
Undefined, unspecified and implementation-defined behavior
(9 answers)
Closed 23 days ago.
It seems that member function clear() of string does remove its content, but the removed contents still can be accessed by operator[] . Here's the example that makes me confused.
#include <iostream>
using namespace std;
int main()
{
string input = "Weird";
cout << "Your Input: " << input << "\n";
input.clear();
cout << "Your Input: " << input << "\n";
cout << "Your Input: " << input[0] << input[1] << input[2] << input[3] << input[4] << '\n';
return 0;
}
The results are:
Your Input: Weird
Your Input:
Your Input: eird
Why this is happenning? If example above is normal, what should I do to completely remove its content? (accessing by input[1] should be '\000')
Accessing elements of a string after calling the method clear invokes undefined behavior.
It seems in your case the class std::string uses its internal buffer defined within the class itself for short strings.
After the call of clear the class just set the first character of the buffer with the terminating zero character '\0;.
To check that string was cleared just output its length as for example
std::cout << input.length() << '\n';
In my simple program to learn C++, I am asking the user their name and then greeting them.
#include <iostream>
int main() {
std::string name;
std::cout << "Enter your name: ";
std::getline(std::cin, name);
std::cout << "Hello, " << name << "!\n";
}
However, here is my result in CLion:
I expected the program to print out a trailing whitespace after the prompt for my name. However, it prints the space after I give the program input. I have only experienced this in CLion, but not in other IDEs. Why is this happening, and how can I fix this?
You need to flush your stream:
std::cout << "Enter your name: " << std::flush;
std::cout is a buffered stream which means that while your write to it isn't immediately written to the underlying device, but it is stored in a buffer. This is done for performance reasons. std::endl has an implicit flush operation, that's why you don't notice this if you always add a std::endl before you request input. Otherwise, like you've seen that can happen.
So I am currently going through Accelerated C++ course on udemy by Jeremy Siek and I was on that tutorial which he is mentioning string concatenation and in one part he gave task to print something (WITHOUT USING NESTED LOOPS) like this:
OUTPUT:
*
**
***
****
(- So I know about for loops from earlier and I am not completely new to C++ programming, but I am just complementing knowledge from earlier and I know that this problem can be solved with nested for loops. But THIS IS NOT MY QUESTION, KEEP READING, BECAUSE I HAVE TO MAKE INTRO INTO MY QUESTION)
Before he made program which is source code like this (WHICH I COMPLETE UNDERSTAND)
int main()
{
cout << "Please enter your name:";
string name;
cin>>name;
string greeting="Hello, " + name + "!";
string spaces(greeting.size(), ' ');
string stars(greeting.size(), '*');
cout << "**" stars << "**" << endl;
<< "* "<< spaces << " *"<< endl;
<< "* "<< greeting << " *"<< endl;
<< "* "<< spaces << " *" <<endl;
<< "**" stars << "**" << endl;
return 0;
}
Now, about my question:
I was trying to do that task he gave, and I came up with something like this:
#include<iostream>
#include<string>
using namespace std;
int main()
{
string star="*";
int br=1;
cout<<star<<endl;
while(br<4)
{
br+=1;
string (newstar.size(br, '*') );
cout<<newstar<<endl;
}
return 0;
}
Now, that program resulted error because of string (newstar.size(br, '*') ); which I don't understand why is that wrong and why is string newstar(br, '*'); correct
without .size and without () ?
I think your intention was to have:
string newstar(br, '*');
istead of:
string (newstar.size(br, '*') );
also maybe increment br after that line, and you could remove 'cout << star << endl;'
See the fill constructor: http://www.cplusplus.com/reference/string/string/string/
The error is
prog.cc: In function 'int main()':
prog.cc:14:13: error: 'newstar' was not declared in this scope
string (newstar.size(br, '*') );
^~~~~~~
prog.cc:14:13: note: suggested alternative: 'star'
string (newstar.size(br, '*') );
^~~~~~~
star
Because you did not declare newstar. The compiler already stops there, but there is more wrong: std::string::size takes no paramters and it is not clear what you expect from writing string ( some_number);.
As I dont really understand the logic of your code I cannot offer you a complete fix, but I can give a hint. This:
std::cout << std::string(4,'*');
constructs a std::string consisting of 4 copies of the character * and prints that on the console.
This question already has answers here:
Printing variable in quotation marks C++
(4 answers)
how to declare char * with input of {"data": "0001"}'
(1 answer)
Closed 4 years ago.
I am beginning my C++ coding with challenges from the book Exercises for Programmers by Brian P.Hogan. I am capable of doing this, it's just I have never come across this int he 4 weeks I have been coding.
I am attempting to write a simple program that prompts the user for a quote, and the author of the quote.
Code:
#include <iostream>
#include <cstring>
int main(int argc, char const *argv[])
{
std::string quote;
std::string author;
std::cout << "Please enter a quote" << '\n';
std::cin >> quote;
std::cout << "Please enter the author" << '\n';
std::cin >> author;
std::cout << author << " said " << ""quote"" << '\n';
return 0;
}
Output:
compile error
With the above code, it compiles wrong. This is because of the double quotation marks
std::cout << author << " said " << ""quote"" << '\n';
The desired output will look something like this
What is the quote? These aren't the droids you're looking for.
Who said it? Obi-Wan Kenobi
Obi-Wan Kenobi says, "These aren't the droids
you're looking for."
Notice the quotation marks on the desired output around the quote (how a quote should really look anyway). I have looked online, but haven't been able to find a solution specifically for C++.
What I am asking, is how do i display text in the terminal with quotation marks around it. (Like this - "hello")
I hope you understand the question. It is my first post and I tried to make it as clear as possible what the issue is.
Thanks in advance.
escape the quote:
https://ideone.com/lcrYlA
#include <iostream>
int main()
{
// your code goes here
std::cout << " hello " << " \"world\"" << std::endl;
return 0;
}
You can of course do:
std::cout << author << " said \" "<< quote << "\"\n";
Quote the quote with \
std::cout << "the character \" is a quote";
You can escape the string using a backslash \" e.g printf("Quotes \"\" ");
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 8 years ago.
Improve this question
So I am reading from the .csv as showed as the result is shown in the image url. When I'm cout-ing everything what's in the file it prints additional " " between the every read cell. So I'm wondering why are these " " cout-ed? And most importantly is there a way to get rid of them?
void Evidenca::IskanjeIstiVnos(vector<Evidenca>& evidenc, vector<Evidenca>& kopir, fstream &Datoteka){
string vrstica;
int StVrstic=0;
int Stej;
string TEMPsemester;
string TEMPletnik;
string TEMPects;
string TEMPocIzpita;
while(!Datoteka.eof()){
getline(Datoteka,vrstica,'\n');
Datoteka >> ws;
StVrstic++;
}
Datoteka.clear();
Datoteka.seekg(0, ios::beg);
cout << "Stevilo predmetov zapisanih v datoteki je: " << StVrstic<<endl; //How many lines are in file
for(int a=0;a<StVrstic;a++){
getline(Datoteka,ImePredmeta,';');
getline(Datoteka,ProfesorImPr,';');
getline(Datoteka,TEMPsemester,';');
getline(Datoteka,TEMPletnik,';');
getline(Datoteka,TEMPects,';');
getline(Datoteka,datumIzpita,';');
getline(Datoteka,TEMPocIzpita,'\n');
cout << ImePredmeta << " " << ProfesorImPr << " "<< TEMPsemester << " " << TEMPletnik << " " << TEMPects << " " << datumIzpita << " " << TEMPocIzpita << endl;
}
getline doesn't make up characters. Those characters are read from your CSV file : you can see them if you open it with a text editor (emacs, vi, notepad..), they are automatically hidden in Excel-like software.