Hello I'm new to programming

I'm stuck on a project I'm working on, could someone tell me what to fix? The goal of the project is this "Write a C++ program that will read a file of integers. Your program should find the file in the current directory of your project. The file name should be random.txt. This file contains a long list of random numbers. Your program should open the file, read all the numbers in the file, and calculates the following:"

My file gets stuck on myfile.open(random.txt) and it gives me an error. What do I need to identify?


#include <iostream>
#include <string>
#include <fstream>
using namespace std;

int main ()
{
//Introduce Program
cout << "This program will tell the average, sum, and count of random.txt" << endl;

//Declaring Variables
double count = 0;
double average = 0.0;
double sum = 0;
int current = 0;

//Open and read file
ifstream myfile
myfile.open("random.txt").
myfile >> current;
while (myfile.fail())
{
sum += current;
myfile >> current;
count++;
}
myfile.close();

//Average the numbers
average = (sum) / (count);

cout << "The sum if the intergers is: " << sum << endl;
cout << "The average of the intergers is: " << average << endl;
cout << "The running total of the intergera is: " << count << endl;
return 0;
}
Last edited on
Well, you are probably getting an error at myfile.open("random.txt") because you forgot the semicolon at the line before that - ifstream myfile see. you forgot the semicolon.
Thanks for the reply however I'm still getting an error there.
You also dont have a semicolon at myfile.open("random.txt"). Instead you have a normal dot for some reason.
Thanks it runs now however my text file named random wasn't made, do you know how I could fix that
Bucky (thenewboston) Has fantastic tutorials on all the basics of c++. Watch some of them - https://www.youtube.com/playlist?list=PLAE85DE8440AA6B83

If you want to learn about files. Its video 64-68

Tip: If you want to create a file you will have to use ofstream, not ifstream.
Thank you so much
My pleasure :) Goodluck to you.
Topic archived. No new replies allowed.