Little help with C++

R

RAGreatti

Ok, I just started programming and I need some help here. Anyone know
how to display the sum of numbers stored in a sequential access file?
So far I have the following text that I pulled from an example, and I
need to fill in the blanks and change all the wrongs. Let me know what
variables to declare if you can. Thanks in advance!!!

#using <mscorlib.dll>
using namespace System;

int main()

{
while (inFile->squares.txt() != -1)
{
record = inFile->ReadLine();
position = record->IndexOf("#", 0);
name = record->Substring(0, position);
sales = Convert::ToInt32(record->Substring(position + 1));
Console::WriteLine("{0}{1}", name->PadRight(15),
sum.ToString("N0")->PadLeft(10));
} //end while

//close the file
inFile->Close();
//end if

return 0;
} //end of main function
 
H

Howard

Ok, I just started programming and I need some help here. Anyone know
how to display the sum of numbers stored in a sequential access file?
So far I have the following text that I pulled from an example, and I
need to fill in the blanks and change all the wrongs. Let me know what
variables to declare if you can. Thanks in advance!!!

#using <mscorlib.dll>
using namespace System;

int main()

{
while (inFile->squares.txt() != -1)
{
record = inFile->ReadLine();
position = record->IndexOf("#", 0);
name = record->Substring(0, position);
sales = Convert::ToInt32(record->Substring(position + 1));
Console::WriteLine("{0}{1}", name->PadRight(15),
sum.ToString("N0")->PadLeft(10));
} //end while

//close the file
inFile->Close();
//end if

return 0;
} //end of main function

Copying garbage code with no idea of what it does is useless. And it won't
get your homework done for you by the folks here.
 
M

Mike Wahler

Ok, I just started programming and I need some help here. Anyone know
how to display the sum of numbers stored in a sequential access file?
So far I have the following text that I pulled from an example, and I
need to fill in the blanks and change all the wrongs. Let me know what
variables to declare if you can. Thanks in advance!!!

#using <mscorlib.dll>
using namespace System;

int main()

{
while (inFile->squares.txt() != -1)
{
record = inFile->ReadLine();
position = record->IndexOf("#", 0);
name = record->Substring(0, position);
sales = Convert::ToInt32(record->Substring(position + 1));
Console::WriteLine("{0}{1}", name->PadRight(15),
sum.ToString("N0")->PadLeft(10));
} //end while

//close the file
inFile->Close();
//end if

return 0;
} //end of main function

I don't know what all that above is, but it's certainly
not standard C++.


#include <fstream>
#include <iostream>

int main()
{
std::ifstream in("squares.txt");

if(in)
{
long sum(0);
long number(0);

while(in >> number)
sum += number;

if(!in.eof())
std::cerr << "Error reading input\n";

std::cout << "Sum is " << sum << '\n';
}
else
std::cerr << "cannot open input\n";

return 0;
}

-Mike
 
V

Victor Bazarov

Howard said:
Ok, I just started programming and I need some help here. Anyone know
how to display the sum of numbers stored in a sequential access file?
So far I have the following text that I pulled from an example, and I
need to fill in the blanks and change all the wrongs. Let me know what
variables to declare if you can. Thanks in advance!!!

#using <mscorlib.dll>
[...]

Copying garbage code with no idea of what it does is useless. And it won't
get your homework done for you by the folks here.

I guess you're mistaken, judging from Mike Wahler's post...
 
R

RAGreatti

Ouch, gotta love friendly people. Homework.... man I'm teaching myself
code, and I am going from nothing. A tutorial gave me the script to
work from and I worked it to that point on my first try. I think it's
sad that you jumped to conclusions like that. It's arrogance of that
type that keeps programmers in the dark as far as personal skills are
concerned. We are viewed as reclusive, it's easy to understand why.....
 
R

RAGreatti

it's keeps telling me I cannot open input when I run the prog. the full
line and address liiks like the following. Let me know if you have any
suggestions, thanks!

std::ifstream in("C:\Richard\Tut11\squares.txt");
 
V

Victor Bazarov

it's keeps telling me I cannot open input when I run the prog. the full
line and address liiks like the following. Let me know if you have any
suggestions, thanks!

std::ifstream in("C:\Richard\Tut11\squares.txt");

In C++ in order to have a backslash in a string literal, you have to
escape it with a backslash.

V
 
T

Thomas Matthews

it's keeps telling me I cannot open input when I run the prog. the full
line and address liiks like the following. Let me know if you have any
suggestions, thanks!

std::ifstream in("C:\Richard\Tut11\squares.txt");

You need to "escape" the escape or '\' (backslash)
character. The \r is the code for carriage return
and \t is the tab character.

Try:
std::ifstream in("C:\\Richard\\Tut11\\squares.txt");
or
std::ifstream in("C:/Richard/Tut11/squares.txt");

Some operating systems allow for forward slashes
instead of backslashes.

Also, please read the FAQ and welcome.txt as referenced
in my signature. These documents will help greatly.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library
 
R

RAGreatti

that worked out wonderfully, I think I got it! Off to try a few more on
my own. Thanks guys!!!
 
P

Puppet_Sock

Ok, I just started programming and I need some help here.

Indeed you do. The second thing you should do is get a copy of
"Accelerated C++" by Koenig and Moo. Read that. If you have
specific questions about it, then come back and ask.

The first thing you should do is read the FAQ for this group.
Since the "welcome" message was posted only Dec. 5, it's kind
of unmannerly of you not to have seen it, but here's the link.
http://www.parashift.com/c++-faq-lite/
Socks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,184
Messages
2,570,978
Members
47,561
Latest member
gjsign

Latest Threads

Top