Basic I/O not working in Visual C++ 2010? - c++

I am trying to make a program to ask for their name, and then say "Hello, (their name)!" back.
Here's my code so far, the "getchar()" is just so it pauses and I can see the output.
#include <iostream>
#include <string>
using namespace std;
int main()
{
string name;
cout<<"What is your name?:";
cin>>name;
cout<<"Hello, "<<name<<"!";
getchar();
return 0;
}
This asks me for input, and I input my name, and then the application closes!
I don't know why and how to fix it! Please help!
EDIT: Found out how to solve it. Finished code:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string name;
cout<<"What is your name?: ";
cin>>name;
cout<<"Hello, "<<name<<"!\n";
system("PAUSE");
return 0;
}

Dietmar wrote the correct answer, unfortunately as a comment for some strange reason.
getchar() is already a hack but I'll let you off. Replacing it with something like system("PAUSE") is even more of a hack, so let's not go there.
Your getchar() is working, but there is still a \n in the buffer from just after the name (remember, you had to type ENTER to submit it!) and this is satisfying getchar() without further user intervention.
You can get rid of that ghost newline:
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
getchar();
However, please consider configuring your execution environment to keep the console window, rather than making your program take on responsibility for this. It's there to take user input and give computed output, not to manage terminal windows.
If you're on Windows, I find cmd.exe /K myProgram to be helpful — the /K runs your program then keeps the command prompt open.

just use the function getch() from <conio.h> !
#include <iostream>
#include <string>
#include <conio.h>
using namespace std;
int main()
{
string name;
cout<<"What is your name?:\n";
cin>>name;
cout<<"Hello, "<<name<<"!\n";
getch();
return 0;
}
Very easy to use, safe and way better than system ("pause")!

Related

How to use the delim parameter from getline

I need to make a program in witch I have to read a text from an input file(ifstream fin("input.in")) and store it until the program meets the "#" character. I know it should be doable using fin.getline, but I can't make the "delim" parameter work. I would find useful an explanation of how doe it work, and an example. I already read this, but I couldn't find an example with fin.getline.
This is what I tried, but it doesn't work:
#include <fstream>
#include <string.h>
#include <string>
using namespace std;
ifstream fin("cod.in");
ofstream fout("cod.out");
char chr[100];
for(int i=0;i<n;i++)
{
fin.getline(chr,'#');
fout<<chr<<" ";
}

I can't read from a file

I wrote a code to make "text1.txt" file. It worked correctly, then I've been trying to read from the file, but every time is_open() function doesn't return true. Even so I copied other codes in the way exactly they are in different compilers, but it never works. How will I solve this:(
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main() {
ifstream file1("text1.txt");
string str;
if(file1.is_open()){
while(getline( file1, str)){
cout<<str;
}
}
else
cout<<"the file is not open"<<endl;
return 0;
}
How are you running your program?
The most common cause of this I've seen is that you're running your program inside an IDE (like Visual Studio), and your current directory isn't where you think it is.
Try putting in the full path to the file and see if your problem disappears.

Netbeans C++ Weird output

#include <iostream>
using namespace std;
int main(){
cout<<"Hello World!"<<endl;
}
I recently installed netbeans 12.0 and when I compile the code above, it says some weird words and numbers and a letter 'H'.
Also the words using, namespace, cout, and endl are underlined and when I hover to it, it says:
unable to resolve identifier + 'word'
This is the output:
PSID=1493
NBMAGIC=1492
H
RUN SUCCESSFUL (total time: 101ms)
As #user4581301 specified that the programs runs and immediately quits so you are not able to see anything. So, we need something to pause the program.
#include <iostream>
using namespace std;
int main(){
cout<<"Hello World!"<<endl;
system("pause");
}

compiler isnt identifying getline() . moreover it isn't letting cin and cout use strings as well

I have been programming since 3 years in c++. I have used compilers like turbo c++, Dev c++, linux & codeblocks.
Recently, I started using Visual Studio 2014 C++ and I'm facing a problem with strings.
using namespace std;
int main()
{
string s;
cout << "enter string: ";
getline(cin, s);
cout << s;
return 0;
}
However, the compiler isn't identifying getline. Moreover, it isn't letting cin and cout use strings as well. The code seems to work with other compilers (e.g. Turbo C++, Dev C++, Linux, CodeBlocks), but it doesn't compile on Visual Studio.
I'm totally confused what could be the problem here.
You are missing #include <iostream>. Without this, you cannot use std::cout or std::cin. Also, you need #include <string> to use std::getline() and std::string.
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s;
cout<<"enter string: ";
getline(cin,s);
cout<<s;
return 0;
}
You can also put system("pause"); before the return statement to have it pause when it prints the output, in case you wanted to see it.

Read line from file: Unable to use getline()

I have the task to write a function that reads a line from a text file (renamed to .dat, however it contains only text), but I am out of options for a solution because of the following points:
I am using Borland C++ Version 5.02, and no, I CAN´T download another compiler because I dont have admin rights on my laptop and the guy who has the needed password isnt there until next week.
The compiler does not accept using namespace std, and also it doesnt accept getline(), no matter if string and iostream are included or not.
I am trying to find a solution or at least the tiniest approach, but I am unable to find one.
So my question is: How do I read a line from a simple textfile without using getline() (cin.getline works, the ones from string or fstream doesnt) ? The textfile contains several lines like these:
1234;12.05.03;08:44:23; XY12-AB;A1-12;Timeout
2345;12.05.03;09:04:34;XY1-CD;A22-9;Connection refused
And the numbers/letters between the ; need to be stored in variables so they can be worked with.
Im not asking for you to write my code, but I am reallyreaylly frustrated and my instructor is no help.
Live long and prosper,
Me.
http://www.cplusplus.com/reference/string/string/getline/
If you cant use (which you really shouldnt)
using namespace std
Then refer to your namespace with :: operator
For example:
std::string
Now try to write your own code using std:: and comment if you still cant do it.
Also, there is a lot of other options than std::getline() to read line of text.
Ref: Read file line by line
Option 1:
Try using the C's fgets() function.
Option 2:
You mention that cin.getline() works. You can freopen stdin with the input file and then cin will point to mentioned file. After that cin.getline() will read from the file:
Downside: After the freopen you will not be able to accept any input from the user.
Example: Note this has not been tried using g++ but I guess it should work with Borland too.
#include <iostream>
#include <stdio.h>
int main() {
char buf[1000];
freopen("somefile.dat", "r", stdin);
while (cin.getline(buf, sizeof(buf)).good()) {
// Now buf contains a line
// Do something with it
}
return 0;
}
Try using
getline(cin >> ws, variableName);
But first, you have to use
using namespace std;
I'm having the same problem while i using multi dimensional array on structs into a file, i have try different ways. But then i tried this one and it's work for me.
So in my case it gonna be like this
#include <iostream>
#include <fstream>
using namespace std;
int main () {
ifstream myFile;
myFile.open("test.txt");
int count = 0;
while (!myFile.eof())
{
getline(myFile >> ws, data[count].string1);
getline(myFile >> ws, data[count].string2);
myFile >> data[count].int1;
for (int i = 0; i < data[count].int1; i++) {
getline(myFile >> ws, data[count].data2[i].string3);
}
count++;
}
return 0;
}
For more : https://www.geeksforgeeks.org/problem-with-getline-after-cin/