I can't figure out why it says this. I am new as you can probably tell... here is the code:
#include <iostream>
using namespace std
int main() {
if (cin >> "hi"
cout << "hello"
return 0;
}
"The thing you were using" (read: your compiler) wanted you to end your using namespace std statement with a semicolon, not to dump one at the start of a function definition.
Your code has a number of extreme and baffling syntax errors, to the extent that it's not even clear what you're trying to accomplish.
Below is a hint to get you started but, from now on, I strongly recommend that you read a good, peer-reviewed C++ book and learn the language before asking any further questions about nonsense code!
#include <iostream>
#include <string>
using namespace std;
int main()
{
string input;
getline(cin, input);
if (input == "hi") {
cout << "hello" << endl;
}
return 0;
}
Related
I have tried running this code in my school and it works like a charm but when I got home, this piece of code suddenly gets error. Can somebody enlighten me about what happened to my code? Is there something that I should correct or add in my code? edited : There were build errors in the code.
#include <iostream>
#include <fstream>
#include <string>
ofstream fileObject;
using namespace std;
int main() {
string username[5];
cout << "Enter username: ";
for (int i = 0; i < 5; i++) {
getline(cin, username[i]);
}
fileObject.open("open.txt", ios::app);
for (int x = 0; x < 5; x++) {
fileObject << username[x] << endl;
}
fileObject.close();
return 0;
}
ofstream fileObject; is above using namespace std, so it's not recongnized as a type.
Either move it below using namespace std, or use std scope, std::ofstream fileObject.
The second option is a better one, read Why is “using namespace std;” considered bad practice?
I don't see how this can run in any C++ compiler, unless you include headers that already recognize std namespace.
Complete newbie here and I had a quick question with regards to newline in my code. So I understand that inserting the using namespace std is bad practice and in writing my program I avoided using cout and cin without the adding the std:: portion first. But I figured since I'm not importing the namespace library I could just comment it out. But when I did this my string name for the variables became unrecognized (red lines underneath it). The red lines disappear when I allow namespace to be imported again. Is the variable string only available in the namespace library?
#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
//using namespace std;
// read and compare names
int main()
{
std::cout << "Please enter two names \n";
string first;
string second;
std:: cin >> first >> second; // read two strings
if (first == second) std::cout << "that's the same name twice! \n";
if (first < second) std::cout << first << " is alphabetically before "
<< second << '\n';
if (first > second) std::cout << first << " is alphabetically after " <<
second << '\n';
return 0;
}
If you don't include using namespace std then you will need to say
std::string first;
std::string second;
because string is defined in the standard namespace also (as well as cout etc).
So yes you are right that string is only defined in standard. string is an object (not a primitive type) and it is this that allows you to do the if(first==second) comparison. Otherwise the "normal" way to compare string is using strcmp() or similar.
I'm new to C++, came from Java (started learning yesterday).
I'm trying to loop over the elements of a vector. For some reason when I do this, it endlessly outputs empty lines.
#include <iostream>
#include <string>
#include <vector>
int main()
{
std::string expression = "5+4";
std::vector<char> characters(expression.begin(), expression.end());
for (char c : characters) {
std::cout << c << std::endl;
}
}
I keep running into this problem, I have no clue why it is doing this.
I tried searching if other people had this problem, but I couldn't find any questions about it or answers... what am I doing wrong?
EDIT:
I'm using MinGW
GIF or it didn't happen
I'm not an expert in CPP, but i assume your code is inserting string to the vector and iterate through the vector, if so, heres the code :
#include <iostream>
#include <string>
#include <vector>
using namespace std; //i add this line to remove the "std" in every line
int main()
{
string expression = "5+4";
vector <char> characters(expression.begin(), expression.end());
for (int i=0; i< characters.size(); i++)
{
cout << characters[i] << endl;
}
}
An explanation that i can tell you is, you do not printing the vectors with its index so it'll have some bug also the looping i use i think more clear to see.
I've seen a couple of solutions to this question, but I'm still running into an issue. I've tried several of the solutions I've seen on this site, but I keep having the same issue. I'm not sure how to make this work, and I'm also not sure why it's not working, since I'm really new to C++.
A brief explanation of what I am attempting to do: I'm writing a simple old-school text-based story/game for a video game design club at my school, and at several points I have the user make decisions, or input things such as their name, which is then used in an output line, as it would be referenced several times. I have had no problem doing that, using something simple like this:
#include <iostream>
#include <string>
#include <limits>
#include <windows.h>
using namespace std;
int main(){
string name, place ;
cout << "???: What is your name? ";
getline (cin, name);
cout << "???: Hello, " << name << "!\n" << "\n";
}
My problem is that I'd like to have the text appear one character at a time, like dialogue, but whenever I try to use something I've seen written by others, it doesn't seem to like it very much. The only one that I tried that I can now find again is this:
#include <iostream>
#include <unistd.h> // include <windows.h> on windows
// function to output as if it was being typed
void type_text(const std::string& text)
{
// loop through each character in the text
for (std::size_t i = 0; i < text.size(); ++i)
{
// output one character
// flush to make sure the output is not delayed
std::cout << text[i] << std::flush;
// sleep 60 milliseconds
usleep(60000); // use Sleep on windows
}
}
int main()
{
type_text("Hej hej hallå!");
}
Apparently there is some sort of conflict regarding my attempt to output the name back to the user when I try to use that code with what I've written. I'm not really sure what the problem is, since I'm so new to C++, can anyone help me out?
Consider using std::this_thread::sleep_for, as it is standard C++11. Example:
#include <iostream>
#include <thread> // standard C++11
// function to output as if it was being typed
void type_text(const std::string& text)
{
// loop through each character in the text
for (std::size_t i = 0; i < text.size(); ++i)
{
// output one character
// flush to make sure the output is not delayed
std::cout << text[i] << std::flush;
// sleep 60 milliseconds
std::this_thread::sleep_for(std::chrono::milliseconds(60));
}
}
int main()
{
type_text("Hello, World!");
}
If you have access to a C++14 compiler, you can simply make use of the std::chrono user-defined literals and have a more "natural" syntax:
using namespace std::literals;
std::this_thread::sleep_for(60ms);
I'm obviously not quite getting the 'end-of-file' concept with C++ as the below program just isn't getting past the "while (cin >> x)" step. Whenever I run it from the command line it just sits there mocking me.
Searching through SO and other places gives a lot of mention to hitting ctrl-z then hitting enter to put through an end-of-file character on windows, but that doesn't seem to be working for me. That makes me assume my problem is elsewhere. Maybe defining x as a string is my mistake? Any suggestions about where I'm going wrong here would be great.
Note: sorry for the lack of comments in the code - the program itself is supposed to take in a series of
words and then spit back out the count for each word.
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <iomanip>
using std::cin;
using std::cout; using std::endl;
using std::sort;
using std::string; using std::vector;
int main()
{
cout << "Enter a series of words separated by spaces, "
"followed by end-of-file: ";
vector<string> wordList;
string x;
while (cin >> x)
wordList.push_back(x);
typedef vector<string>::size_type vec_sz;
vec_sz size = wordList.size();
if (size == 0) {
cout << endl << "This list appears empty. "
"Please try again." << endl;
return 1;
}
sort(wordList.begin(), wordList.end());
cout << "Your word count is as follows:" << endl;
int wordCount = 1;
for (int i = 0; i != size; i++) {
if (wordList[i] == wordList[i+1]) {
wordCount++;
}
else {
cout << wordList[i] << " " << wordCount << endl;
wordCount = 1;
}
}
return 0;
}
If you're on windows ^Z has to come as the first character after a newline, if you're on a unixy shell then you want to type ^D.
The input portion of your code works. The only real problem I see is with the loop the tries to count up the words:
for (int i = 0; i != size; i++) {
if (wordList[i] == wordList[i+1]) {
The valid subscripts for wordList run from 0 through size-1. In the last iteration of your loop, i=size-1, but then you try to use wordList[i+1], indexing beyond the end of the vector and getting undefined results. If you used wordList.at(i+1) instead, it would throw an exception, quickly telling you more about the problem.
My guess is that what's happening is that you're hitting Control-Z, and it's exiting the input loop, but crashing when it tries to count the words, so when you fix that things will work better in general. If you really can't get past the input loop after fixing the other problem(s?), and you're running under Windows, you might try using F6 instead of entering control-Z -- it seems to be a bit more dependable.
I pretty much always use getline when using cin (particularly when what I want is a string):
istream& std::getline( istream& is, string& s );
So, you'd call getline(cin, x) and it would grab everything up to the newline. You have to wait for the newline for cin to give you anything anyway. So, in that case, your loop would become:
while(getline(cin, x))
wordList.push_back(x);
cin does not accept blank space or line breaks so execution of cin does not complete unless you enter something , here is a test program that gives you what you want
#include "stdafx.h"
#include<iostream>
#include <string>
#include <sstream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
string str = "";
while(std::getline(cin, str) && str!="")
{
cout<<"got "<<str<<endl;
}
cout<<"out"<<endl;
cin>>str;
return 0;
}