So i got a little beginner problem here.
I cant seem to print out a string with char and integers in it.
#include <iostream.h>
#include <conio.h>
Main()
{
Char date[20];
Clrscr();
Cout<<"enter date: ";
Cin>>date;
Cout<<endl;
Cout<<date;
Getch();
Return 0;
}
My input here is suppose to be :
January 1-5,1999
But all it shows is :
January.**
Use getline(). Otherwise it cuts it after a space. Also, do not use capitals for cout, etc.
Like
string date;
getline(cin,date, '\n');
The answer by #Caspar Wylie is correct but if are using a very old/outdated compiler(guessed from conio.h and iostream.h header files) then try this
#include <iostream.h>
#include <conio.h>
int main()
{
char date[20];
cin.getline(str,20);
cout << date << endl;
getch();
return 0;
}
Related
In my program it should be an option to ask a user for input, and then save input string into the file. My problem is, - when I put cin in any of it forms, inside Switch, program will stuck circling indefinitely, right after i press enter after finish typing new text. What could cause the problem?
#include <iostream>
#include <fstream>
#include <iterator>
#include <string.h>
#include <time.h>
#include <stdlib.h>
#include <algorithm>
#include <chrono>
#include <random>
#include <vector>
using namespace std;
void changePlainText()
{
ofstream nFile("plaintext.txt");
string newText;
cout << "Enter new plain text" << endl;
getline(cin, newText);
nFile << newText;
nFile.close();
}
int main()
{
int uInput = 0;
do
{
printf("2.Change content of the plain text file: \n");
cin >> uInput;
switch (uInput)
{
case 1:
break;
case 2:
changePlainText();
break;
}
} while (uInput != 5);
cout << "Closing program" << endl;
system("pause");
}
After I type something in console and press enter, the program enters never ending circle. It still stuck even if I just write simple cin >> i, in switch case.
Take a look at this question. I debugged your code and I experienced that exact behavior. The accepted answer explains quite well what's happening and also provides a solution.
I tried with boost, I changed
cin >> uInput;
to
string inputString; // so you know what inputString is
getline(cin, inputString);
uInput = boost::lexical_cast<int>(line);
and it's working fine now.
Why this doesn't print the first word of sentence?
#include <iostream>
#include <string>
int main()
{
std::string sentence;
std::cout<<"Enter sentence: ";
std::cin>>sentence;
std::getline(std::cin,sentence);
std::cout<<sentence;
return 0;
}
If I enter
"This is text"
output would be
" is text"
You dont need the first cin (std::cin>>sentence;), this will solve your problem
#include <iostream>
#include <string>
int main()
{
std::string sentence;
std::cout<<"Enter sentence: ";
std::getline(std::cin,sentence);
std::cout<<sentence;
return 0;
}
std::cin>>sentence;
This line of code takes the first word you input.
Remove it and you are good to go
This is my code!
#include "stdafx.h"
#include<iostream>
#include<conio.h>
void main()
{
clrscr();
int no;
cout<<"Enter a number";
cin>>no;
getch();
}
and I get this error here!
I think I might have to download some extra visual studio c++ related directories, but still some suggestions please
clrscr() is not a standard function. Visual Studio does not have it. However, MSDN does document how to clear the screen using system("cls"), or FillConsoleOutputCharacter() and FillConsoleOutputAttribute().
As for the cin/cout errors, you need to prefix them with the std:: namespace qualifier, eg std::cin and std::cout, or use a separate using namespace std; statement in your code below the header #include statements.
Try this:
#include "stdafx.h"
#include <iostream>
#include <cstdlib>
#include <conio.h>
void clrscr()
{
std::system("cls");
}
int main()
{
clrscr();
int no;
std::cout << "Enter a number";
std::cin >> no;
getch();
return 0;
}
Or:
#include "stdafx.h"
#include <iostream>
#include <cstdlib>
#include <conio.h>
using namespace std;
void clrscr()
{
std::system("cls");
}
int main()
{
clrscr();
int no;
cout << "Enter a number";
cin >> no;
getch();
return 0;
}
Right now your c++ program doesn't know what clrscr(); is..
you have to define that function. To define it, see #Remy Lebeau's answer.
One quick solution instead of creating a function to clear out the screen is to simply cout a bunch of blank spaces.
so in your main you can simply put :
std::cout << string(50, '\n');
I'm using Visual C++ 6.0 and currently created a program that will print the output stored in string.
The problem is when I entered words with space, only the first word is visible in the output.
Example:
Enter your address: new york
new
Press any key to continue
I want this:
Enter your address: new york
new york
Press any key to continue
Also, I tried to use getline but when I entered words, It will first print blank space then stored the last output before the current one.
Here's my code:
#include <iostream>
#include <string>
using namespace std;
void main()
{
string address1;
cout<<"Enter your address:";
cin>> address1;
// getline(cin, address1); code when using getline
cout<<address1<<"\n";
}
you are doing it correct, but the main problem is that you are using cin while you should avoid it and use getline(cin,address1) because cin will only take a single word and it will not take anyhting which you type after space. On the other hand getline(cin,address1) can take a complete sentence along with spaces
Read the comments and use this code
#include <iostream>
#include <string>
using namespace std;
int main()//using int main()
{
string address1;
cout<<"Enter your address:";
//cin>> address1; Don't use it
getline(cin, address1);//use this
cout<<address1<<"\n";
return 0;//returning an integer 0
}
Use std::getline (std::cin, address1);, not cin. Because cin takes space as delimiter.
What about this one? Kind of getline concept, presuming newline character is '\n', change as required according to your platform, unix or windows etc
int main()
{
string addrpart, address1;
cout<<"Enter your address:";
cin>> addrpart;
while (addrpart != "x") {
address1 += addrpart + " ";
addrpart = "x";
cin>> addrpart;
}
cout<<address1<<"\n";
}
Here you go
#include <iostream>
#include <string>
using namespace std;
int main( int argc, char** argv )
{
string userinput;
cout << "Enter your address:";
getline ( cin, userinput );
cout << userinput;
return 0;
}
$ g++ a.cpp -o app
$ ./app
Enter your address:new york
new york
Which command lets me to cout my new random numbers by presing enter? I tried to write system("pause") but then comes line "press any key to continue" which I dont like. Is there any possibility to just press enter button and see numbers one after one?
Here's the program code:
#include <iostream>
using namespace std;
#include <stdlib.h>
#include <ctime>
int main()
{
int i, k;
srand(time(0));
for (i = 0; i < 20; i++)
{
cout << (rand() % 8) << endl;
}
}
#include <limits>
// ...
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
The advantage over reading a single character is that this will make sure that no stray characters are left over into the buffer.
Here is a slightly more modern C++ take on it:
#include <iostream>
#include <ctime>
#include <random>
int main()
{
std::mt19937 rng(std::time(0));
std::uniform_int_distribution<int> gen(0,7);
for (int i=0;i<20;i++)
{
std::cout << gen(rng);
std::string line;
std::getline(std::cin, line);
};
}
You can simply use getchar() which will read one keypress and then continue. If you want to specifically break on the enter key, use something like while (getchar() != "\n");
add cin >> SomeVariableThatCouldAcceptAnythingInCaseYouPressSomethingElseBeforeTheEnterKey either before or after the cout, whichever you like.
Just read a string, right?
#include <string>
....
for (i=0;i<20;i++)
{
std::string str;
cin >> str; // this should pause until the user presses enter
cout << (rand()%8) << endl;
};
What about a simple cin.get()? No need for any temporary variable nor any C functions.