My output will not write to a file [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 6 years ago.
Improve this question
Here is the code i wrote:
#include <stdio.h>
#include<stdlib.h>
int main(void)
{
float x;//the data value
float sum=0.0;//the running total of the values readd
int n=0;//the number of data values read
float cube;
FILE* fin = fopen("sumcubes.in", "r"); /* open for reading */
FILE* fout=fopen("sumcubes.c","w");
while(1){
fscanf(fin,"%f",&x);
if(feof(fin))break;
n++;
cube=x*x*x;
sum=sum+cube;
}
fprintf(fout,"There are %d values in the file:",n);
fprintf(fout,"The sum of cubes of these values is:%f",sum);
fclose(fin);
fclose(fout);
system("sumcubes.in");
system("sumcubes.c");
system("pause");
return 0;
}
I was expecting it would read the values in the file and then cube each value. after cubing each value in file it will get the sum of these cube values. however nothing was printed to the output.

When I ran your code (after Ken Y-N fixed the typo), it worked just fine, except that the three system() calls generated "command not found" error messages. The file sumcubes.c contained the expected results, though why you would give that file a .c extension baffles me. Perhaps the file sumcubes.in is not in your current directory when you are running the executable? When I tried that, I got a segmentation fault, but since you don't check the results of fopen(), if the input file does not exist (or is not in your current directory), anything could happen.

Related

unable to run cin before cout statement [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 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.

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 do I tell a program where to start reading a .txt file? [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 6 years ago.
Improve this question
I want my program to be able to remember where it left off in a .txt file in order to proceed to the next input upon reiteration through a loop.
For instance, a text file containing:
Apples
Bananas
Oranges
would be accessed through a function GetItem() that appends the next file input into a vector of items. How do I make the function add Apples the first time, Bananas the second time, and Oranges the third iteration? As of now, each call to GetItem() keeps adding the first element to the vector, giving a vector containing:
Apples
Apples
Apples
Because the file keeps opening from the beginning. Any help would be appreciated.
This is a simplified version of lengthy amounts of code that I could include, but would distract from the main purpose of the question. If the code is needed, I would be happy to include it.
vector<Item*> AddItemToInventory(vector<Item*> inventory) {
if (inptLctn == 'f') {
inptFile.open("TestFood.txt");
if (!inptFile.is_open()) {
cout << "Could not open file." << endl;
exit(1);
}
inptFile >> usrInptName;
inptFile >> usrInptQnty;
inptFile >> usrInptExpr;
inptFile >> usrInptPrice;
}
prdc = new Produce;
prdc->SetName(usrInptName);
prdc->SetQuantity(usrInptQnty);
prdc->SetExpiration(usrInptExpr);
prdc->SetPrice(usrInptPrice);
inventory.push_back(prdc);
return inventory;
}
Open the file before you use it and close it after you are done using it. The problem is that you are continually closing the input file so when you re-open it, it starts at the beginning again.
You should only open and close your input file once.

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.