1.

Solve : HELP FOR C++ BEGINNER?

Answer»

I just started learning C++ and i got this assignment to write a program that writes:

This (") is a quote, and this (\) is a backslash.

I wrote the program to output Hello, World! on the screen and that worked fine. I have tried many other STATEMENTS and they all worked. The above mentioned statement however will not work.

Here is what i have done so far:

#include

using namespace std;

int main()

{

std::COUT << "hello world" < std::cout << "This (")is a quote, and this (\) is a backslash." << std::endl;
return 0;
}

I cant find anything wrong with it. Can anyone help?

Thanks in advanceNotice how your string contains a quote but C++ also uses a quote to say where the string starts and ends:
std::cout << "This (")is a quote, and this (\) is a backslash." << std::endl;

This is a problem because when C++ goes along that line it sees the first quote as "String starts here", then it reaches the second quote and thinks "String ends here". Therefore it gets confused because it sees your line as containing 3 quotes and doesn't know which is inside the string and which are there to start and stop it. The backslash will also be causing issues which you will find out later.

What you need to do is to use "Escape sequences" which is a way to tell C++ to consider certain characters in strings as simple characters rather than them haiving some sort of special meaning. You can read more about how this works online.Dude i read the the information to SOLVE the problem many times and did not get it till i followed your directions.

Thanks for putting my brain back on track.


I got a new problem. I am trying to get this program to run but it WONT. Can you guys tell me what i am doing wrong?

#include
#include


int main()
{

std::cout << "Please enter you first name.";
std::string name;
std::cin >> name;

const std::string greeting = "Hello,"+name+"!";
const std::string spaces(greeting.size(),'');
const std::string second = "*"+spaces+"*";
const std::string first(second.size(),'*');

std::cout < std::cout < std::cout < std::cout <<"*"< std::cout < std::cout <
return 0;
}


By the way i am using code blocks to write the programs.

I found the solution to my second problem.

The problem was with line thirteen. This is what the CORRECT line 13 should be:

const std::string spaces(greeting.size(),'*');

instead of this:

const std::string spaces(greeting.size(),'');

This is what i think is happening in the correct line 13 in my own words:

It calculates the amount of characters that make up greeting and then multiplies the number of characters that make greeting by the amount of * (in this case one) to get the total amount of * to put in spaces.

Well that is what i thought until i tested my theory.

Is there anyway anyone can simplify what is happening in line 13. I would like to modify the program but line 13 is not fully understood.

Or should i just wait until i learn a little more.
If you guys think it involves too much advanced stuff and I should wait unil i know some more just say so.

Thanks again for your time.



Discussion

No Comment Found