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
As one can see, I am just starting out with C++ and just began my hello world program.
#include <iostream>
using namespace std;
int main() {
cout << "Hello, World!" << end1;
cout << "Hooray!" << end1;
system("PAUSE");
return 0;
}
But for some reason, unknown to me, I am getting an error on both of the cout lines, saying end1 was undeclared! How do I fix this?
end1
should be:
endl
You used a 1 (the number) instead of l (the letter).
that’s endL just like end LINE :P
It should be endl (end line), not end1 (end one?):
cout << "Hello, World!" << endl;
cout << "Hooray!" << endl;
endl represent end line, it is not end1.
You have a typo. Try endl instead of end1.
You should write endl instead of end1 (So make the one to a small "L")
It should be endl and not end1.
replace end1 by endl and it'll work just fine! :)
To avoid typos like the one you experienced,
instead of using "endl", you can use 'new line' by inserting '/n' inside the quote.
Example:
cout << " Hi, this is how you do it /n";
You only need to replace your
end1
with
endl
Thus you have a syntax error that you need to fix by replacing the one('1') with an 'l', thereby fixing your program. It should compile and run successfully now.
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 2 years ago.
Improve this question
The goal is to code a simple program that reads two strings and compare them to each other. The error (C2679 binary '>>') is pointing to the input line of the code, the 'cin' line. I've done the #include and the using declaration, but maybe I am missing a notation that isn't apparent to me:
#include <iostream>
#include <string>
using std::cin;
using std::cout;
using std::endl;
using std::string;
int main(){
// define two strings
string s1, s2;
// read two strings
cin >> s1 >> s2 >> endl;
// compare two strings to determine if equal or report the larger
if(s1 != s2){
if(word1 > word2)
cout << s1 << " is larger than " << s2 << endl;
else
cout << s2 << " is larger than " << s1 << endl;
} else
cout << "The words are equal in size" << endl;
return 0;
}
Thanks in advance!
Using endl with cin doesn't make sense. >> endl should be removed.
word1 and word2 are not declared here. It seems if(word1 > word2) should be if(s1 > s2).
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 4 years ago.
Improve this question
I'm beginning to learn c++ with the free online codecademy course, and I'm not sure if it's a bug with their IDE or an error in my code.
#include <iostream>
#include <string>
int main() {
for (int i = 0; i > 0; i--) {
std::cout << i << " bottles of beer on the wall\n";
std::cout << i << " bottles of beer\n.";
std::cout << "take one down and guzzle it down\n";
std::cout << i - 1 << " bottles of beer on the wall.\n\n";
}
}
This is supposed to loop those strings until the number gets down to 1. Thank you for your help.
Your variable i is never bigger than 0.
I think that you meant to write int i = 10;
#include <string>
int main() {
for (int i = 10; i > 0; i--) {
std::cout << i << " bottles of beer on the wall\n";
std::cout << i << " bottles of beer\n.";
std::cout << "take one down and guzzle it down\n";
std::cout << i - 1 << " bottles of beer on the wall.\n\n";
}
}
I am not sure what your code is supposed to loop through. but this will run 10 times. you say you have a string. so try to get a variable to assign to a function string.length() to get the length assigned to i and then the loop will run as many times as the number of characters in the string.
the above code will run 10 times though
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
I'm quite a newbie (started to learn about coding just like 2 weeks ago) and I'd really appreciate some help to explain why my code isn't working.
I wrote a simple code to calculate the probability. That's not really important part. The code requires some user input so in order to make it kinda foolproof, I wrote two if statements for various kind of wrong input (wrong data type, number too high, number too low) to throw an error message and kill the program. And if the input is right (means none of the conditions of the if statements is true), it should call a function diceTwo(des).
However, if I input number 8 - which should be allright - for some reason that I can't figure out - it triggers the number too high/low if statement, so no matter what number I enter, the function never gets called.
cin >> des;
if (cin.fail())
{
cin.clear();
cin.ignore(100, '\n');
cout << "Invalid input! Use only whole positive numbers!" << endl;
cout << "Try again." << endl;
return -1;
}
if (des> 12 || des< 1);
{
cout << "Invalid input!" << endl;
cout << "Remember the only possible values are from 2 to 12!" << endl;
cout << "Try again." << endl;
return -1;
}
diceTwo(des);
cout << "The probability of this roll is " << x << "%" << endl;
This is the part that makes troubles. I already tried to split the problematic if statement into two (one for <1, one for >12), or adding a new if statement for des>1 && des<12 to call the function, none of it worked. I worked with if statements to provide foolproofness for user input few times before, always worked well, so I really can't seem to find what's wrong this time. Anyone could tell me how to fix it please?
You have a semi colon after the if statement. Look CLOSELY:
if (des> 12 || des< 1); //<----- semi colon
{
cout << "Invalid input!" << endl;
cout << "Remember the only possible values are from 2 to 12!" << endl;
cout << "Try again." << endl;
return -1;
}
The semi colon terminates the if statement, so that your program just continues to the next line, which, happens to be your invalid input line. Remove the semi-colon and you're golden :)
Mistakenly added ; in if
if (des> 12 || des< 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 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;
}