Quantcast

Maximum PC

It is currently Mon Sep 01, 2014 2:21 pm

All times are UTC - 8 hours




Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: Help with Lab
PostPosted: Mon Oct 06, 2008 7:09 am 
Team Member Top 1000
Team Member Top 1000
User avatar

Joined: Thu Sep 11, 2008 4:10 pm
Posts: 868
Quote:
Assignment: for Comp Sci 1 C++

Write a function FillArray, which takes two arguments, the first is the name of a file, the second is an array of strings of size 500 with no data in it. Your function should open the file. The file will consist of strings each separated by whitespace.

Read the strings into the array until done. The return value should be the number of strings in the array. If for any reason hte function cannot open the file, it should return -1. THe function should close the file before it returns.

Use the song from Homework1 as your test data.


Code:
//Fill array

#include <iostream>
#include <string>
#include <fstream>

using namespace std;

int FillArray(string file, string large)
{
   int counter;
   ifstream song1;

   if (!song1.is_open)
   {
      return -1;
   }

   return 0;
}

int main ()
{
   string large(500);
   string file;
   int counter;

   cout << "Enter name of text file to open:" endl;
   cin >> file;

   counter = FillArray(file, large);

}


I have the file saved as song1. (it's the limerrick about the a queen...my professor is british.) So what should I do from here. I'm stuck


AJ


Top
  Profile  
 
 Post subject:
PostPosted: Mon Oct 06, 2008 7:29 am 
SON OF A GUN
SON OF A GUN
User avatar

Joined: Mon Nov 01, 2004 5:41 am
Posts: 11605
What exactly are you stuck on?


Top
  Profile  
 
 Post subject:
PostPosted: Mon Oct 06, 2008 7:44 am 
Team Member Top 1000
Team Member Top 1000
User avatar

Joined: Thu Sep 11, 2008 4:10 pm
Posts: 868
Never mind ... got it... I wasn't sure where to proceed.. My friend helped me.

Code:
//Fill array

#include <iostream>
#include <string>
#include <fstream>

using namespace std;

int FillArray(string file, string large[500])
{

   int counter;
   counter = 0;
   ifstream song1;
   song1.open(file.c_str());

   if (!song1.is_open())
   {
      return -1;
   }
   while (!song1.eof())
   {
      song1 >> large[counter];
      counter++;
   }
   return counter -1 ;
}

int main ()
{
   string large[500];
   string file;
   int counter;

   cout << "Enter name of text file to open:" << endl;
   cin >> file;

   counter = FillArray(file, large);

   cout << counter << endl;

   return 0;
}


Thanks anyways.

AJ


Last edited by CTskifreak on Mon Oct 06, 2008 4:43 pm, edited 1 time in total.

Top
  Profile  
 
 Post subject:
PostPosted: Mon Oct 06, 2008 9:42 am 
Java Junkie
Java Junkie
User avatar

Joined: Mon Jun 14, 2004 10:23 am
Posts: 24222
Location: Granite Heaven
For the record and your future reference: we do not do homework here.

We will answer specific questions, but only if it is evident that doing so will not ruin the point of the homework exercise.

I am thinking we need to make a sticky.


Top
  Profile  
 
 Post subject:
PostPosted: Mon Oct 06, 2008 10:47 am 
SON OF A GUN
SON OF A GUN
User avatar

Joined: Mon Nov 01, 2004 5:41 am
Posts: 11605
Good call.

Need HW Help? Look here first.

Have the rules in the first post and let homework discussions happen inside that thread. Lock them outside. Should be simple enough.


Top
  Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC - 8 hours


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group