unable to run cin before cout statement [closed] - c++

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I have just started working in Visual Studio Code.
I am facing a problem that my code is not working in VS Code even though it is working in an online compiler. A basic "hello world" program is running fine in VS Code.
I am using the mingwin g++ compiler running the code using VS Code (Ctrl + Shift + B).
My code is given below. If I uncomment the cout statement, the complete code will work fine.
#include <iostream>
using namespace std;
int main()
{
int t;
//cout << "hello-world";
cin >> t;
cout << t;
}

The reason why you only see a blinking cursor waiting for an input from user is because your program runs cin first before the cout. If you uncomment line 6 in your code, you will see "hello-world" displayed first before the blinking cursor. I see no error in your code even you uncomment that one.

Related

ERROR: Thread 1: EXC_BAD_ACCESS (code=1, address=0x68) FIX: PLACE THE IN AND OUT FILES IN THE WORKING DIRECTORY OF THE PROJECT [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I get this error:
Thread 1: EXC_BAD_ACCESS (code=1, address=0x68)
on the following line:
while(fscanf(f,"%d", &a[x])==1)
1st time trying to do my class homework in Xcode, used code blocks before and managed to run this same code on it, any help is much appreciated!
#include <cstdio>
int a[1001];
int main()
{
int aux1,aux2,aux3,x=0;
FILE *f,*g;
f=fopen("1.in","r");
g=fopen("2.out","w");
while(fscanf(f,"%d", &a[x])==1) //HERE
{
aux1=a[x];
aux2=0;
aux3=0;
while(a[x]!=0)
{
aux2=aux2*10+a[x]%10;
a[x]=a[x]/10;
aux3=aux3*10+9;
}
if(aux3-aux2==aux1)
fprintf(g,"1 ");
else fprintf(g,"0 ");
x++;
}
fclose(f);
fclose(g);
return 0;
}
To find out if you put the in and out files in the working directory you must try to print the read numbers in a terminal, if it doesn't print anything then I your .in and .out files are outside the working directory.
Debug:
After this line :
fscanf(f,"%d", &n);
do:
printf("%d", n);
if it prints the number well, guess your files are in the working directory, just make sure .out and .in are in the same directory and that's it.
Make sure that you actually opened 1.in file by checking what open() had returned. Make sure that you do not go outside of array bounds, if x reach 1001, something bad may happen, it's an Undefined Behaviour.
The problem was that I placed 1.in and 1.out outside the working directory, to fix this had to go to Product>Scheme>Edit Scheme>Run>Options and set a custom "Working Directory" where I placed the 1.in and 1.out files. Thanks for the help guys.

C++ Text Files usage [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I am currently learning how to use text files, but when i put a condition like the one in the code below, even if the condition is true it won't try the while part. Can anyone help out?
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
string s, d;
ofstream k("t1.txt");
int a;
cin >> a;
if (a > 3)
{
while (k.is_open())
{
getline(cin, s);
k << s;
k.close();
}
}
ifstream r("t1.txt");
while (r.is_open())
{
getline(r, d);
cout << d;
r.close();
}
}
I ran the code in gdb, it runs perfectly. It opens the file, gets the < cr > character from stdin, writes that to the first line which wipes out the first line, then tries to read the first line which is empty so there is no output. Good job 8).
You're just having trouble understanding your own expectations, your code works fine, so just think a little bit more about what you think it's supposed to do.
You'll also need to examine the contents of your text file before and after running the code, and try entering some text in the text file before you start the program, to see what happens to the text.
The getline where you try to read from the console is the issue, that returns without asking for input so it ends up giving you a blank line to write to the file.
From this link:
http://www.cplusplus.com/forum/beginner/39549/
There's a similar question there, and the first of the comments mentions this about the behavior of getline:
std::cin leaves the newline character in the buffer after pressing enter, and getline just grabs it and keeps going, that's why getline doesn't block to wait for input. .......
That's why it's not "doing anything" - it's not asking you for input, so the program has nothing to output and it looks like it's not working.
(this, by the way, is why programmers are snarky - we have to deal with stupid little behavioral issues of machines and we forget that PEOPLE are not normally like this and that PEOPLE hate being held to these kinds of standards)
I put a second getline in your code right after hte first one and now it asks for and outputs a string that I type in and sticks it in the file.
To run the program in gdb, do the following:
g++ -g < your cpp file > -o < whatever you want the binary to be called >
like this:
g++ -g myfile.cpp -o myrunnableprogram
this creates a binary with symbols that you can open in gdb
then
gdb myrunnableprogram
then in gdb
start
next
next
next
....
these commands will start the program and pause it at the first breakable spot, then each time you type next you will step to the next command and you can poke around at variables to see what's going on.
read up on gdb, it's really useful.

How to get a C++ program built in Visual Studio Code to accept user input [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I'm familiar with the Visual Studio IDE on Windows, and I wanted to give VSCode a shot. For my current situation, I am running VSCode on Ubuntu 64-bit. I installed VSCode, and then installed the C/C++ Extension. Next, I wrote a very simple C++ program that outputs "Hello World", and then asks the user to input their name. The program would then say/output hello to that user. Here's the issue: I am having trouble figuring out how to actually provide the user input to the program. During runtime, I see the cursor blinking in the output panel after the "Hello World" output, but when I press any keys on my keyboard to provide user input, nothing appears, and nothing happens. Any help would be greatly appreciated.
#include<iostream>
#include<string>
using namespace std;
int main() {
string name = "";
cout << "HELLO WORLD" << endl << endl;
cin >> name;
cout << "Hello " << name;
return 0;
}
Better use terminal then the Visual Studio Code Run feature

How to explain the strange output by "puts" in c++? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I am reading a piece of code written by others, there is one line like this:
cout << "Data Loaded" << endl;
it seems nothing strange, however, the actually output is:
[18607330327, 18790481919] [19144201237, 19327352831] [20754813973, 20937965567] [21291684885, 21474836479] [21474836482, 21653864362] [22011707392, 22190735274] [23622320128, 23801348010] [24159191040, 24338218922] [27197264917, 27204255743] [27205653525, 27212644351] [27230819349, 27230959453] [27233615872, 27235153757] [30064771072, 30067638186] [30073159680, 30076026794] [30098325504, 30098440106] [30098456576, 30098536200] Data Loaded
where does the extra output come from? if I comment that line, then, nothing is output.
I then include the <cstdio> and replace that line by puts("Data Loaded"), still, the extra info get printed.
cout is a buffered output stream, and endl not only creates a new line, it also flushes the buffer. Without the flushing of the buffer it might happen that you do not see the output of a previous cout.

"cin" do not work when I use it in Visual Studio 2013 [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
when I try to compile a simple program like this:
// i/o example
#include <iostream>
using namespace std;
int main()
{
int i;
cout << "Please enter an integer value: ";
cin >> i;
cout << "The value you entered is " << i;
cout << " and its double is " << i * 2 << ".\n";
return 0;
}
it starts, but when I put the integer value and press ENTER, it closes. I really don't know why it happens since it works good when I use Dev C++. And if I want to just compile my program I need to press ctrl+f5? Thank you.
What is likely happening is that the computer returns the two cout lines and then runs return 0, shutting down the program.
Since there is no pause, it just executes the code and exits, without waiting for further user input. This is what is giving the appearance of the program not working well.
If you put a breakpoint, or run the code in debug mode, or add a pause, your code will show the output as well.
And for future reference, you'll want to keep your questions to just one question per question. The Ctrl + F5 question can be found via Google or by searching SO.