reading files and strings

Post questions regarding programming in C/C++ in Linux/Unix.

Moderators: Darobat, RecursiveS, Dante Shamest, Bugdude, Wizard

reading files and strings

Postby Urza9814 » Sun Aug 01, 2004 1:25 pm

I need to read a file, and I need to use strings...and I'm getting errors in both...here's the code:

Code: Select all
#include <stdlib.h>
#include <ctime>
#include <iostream>
#include <string>
#include <fstream>

long real_hours;
long hour;
int day;
int month;

using std::cout;

int main()     //line 35
  {
  real_hours = (((time (NULL)) / 60) / 60);
  cout << real_hours << "\n";
  cout << real_hours - 303148<< "\n";
  hour = real_hours - 303148;
  cout << hour << "\n";
  while (hour >= 240)
    {
    hour -= 240;
    day += 10;
    }
  while (hour >= 24)
    {
    hour -= 24;
    day += 1;
    }
  cout << "Time is: " << hour << "\n";
  month = 8;
  cout << "Day is:" << month << day + 1 << "\n";
  }

int changebg()         //line 57
  {
  string filename1;
  string filename2;     //line 60
  int time1;
  int time2;
  char buffer[256];
  ifstream settings ("settings.txt");
  if (! settings.is_open())        //line 65
    {
    cout << "Error opening file"; exit (1);
    }
  while (! settings.eof() )
    {                                 //line 70
    settings.getline (buffer,100);
    filename1 << buffer << endl;
    settings.getline (buffer,100);
    filename2 << buffer << endl;
    settings.getline (buffer,100);    //line 75
    time1 << buffer << endl;
    settings.getline (buffer,100);
    time2 << buffer << endl;
    }
  cout << filename1 << "\n" << filename2 << "\n" << time1 << "\n" << time2 << "\n";  //line 80
  }               


And here's the errors:
Code: Select all
main.cpp: In function `int changebg()':
main.cpp:59: error: `string' undeclared (first use this function)
main.cpp:59: error: (Each undeclared identifier is reported only once for each function it appears in.)
main.cpp:59: error: syntax error before `;' token
main.cpp:64: error: `ifstream' undeclared (first use this function)
main.cpp:64: error: syntax error before `(' token
main.cpp:65: error: `settings' undeclared (first use this function)
main.cpp:72: error: `filename1' undeclared (first use this function)
main.cpp:72: error: `endl' undeclared (first use this function)
main.cpp:74: error: `filename2' undeclared (first use this function)
main.cpp:76: error: invalid operands of types `int' and `char[256]' to binary `operator<<'
main.cpp:78: error: invalid operands of types `int' and `char[256]' to binary `operator<<'
main.cpp:81:4: warning: no newline at end of file
gmake: *** [main.o] Error 1
*** failed ***
(22:24:12) Urza4189: I programmed like a motherfucking beast...I don't know how I did it...but I organized and commented like an 8 year old - not at all
User avatar
Urza9814
 
Posts: 185
Joined: Sun Nov 02, 2003 2:18 pm
Location: Indiana, PA

Postby Kybo Ren » Sun Aug 01, 2004 2:20 pm

You need to add using namespace std; instead of just cout.
Kybo Ren
C++ Beginner
 
Posts: 2049
Joined: Wed Feb 11, 2004 9:28 pm

Postby Urza9814 » Sun Aug 01, 2004 6:42 pm

I have no idea WTF is going on now...here's the code:
Code: Select all
#include <stdlib.h>
#include <ctime>
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>

string command;
long real_hours;     //line 30
long hour;
int day;
int month;
string filename1;
string filename2;
int time1;
int time2;

int set();
int str2int();    //line 40

using namespace std;

int str2int(string in)
  {
  istringstream converter(in);
  int out;
  converter >> out;
  return out;
  }                  //line 50

int main()
  {
  while(true)
    {
    real_hours = (((time (NULL)) / 60) / 60);
    cout << real_hours << "\n";
    cout << real_hours - 303148<< "\n";
    hour = real_hours - 303148;
    cout << hour << "\n";       //line 60
    while (hour >= 240)
      {
      hour -= 240;
      day += 10;
      }
    while (hour >= 24)
      {
      hour -= 24;
      day += 1;
      }                    //line 70
    cout << "Time is: " << hour << "\n";
    month = 8;
    cout << "Day is:" << month << day + 1 << "\n";
    set();
    if(hour == time1)
      {
      command = "fbsetbg -c ";
      command += filename1;
      cout << command;
      system(command);      //line 80
      }
    if(hour == time2)
      {
      command = "fbsetbg -c " + filename2;
      cout << command;
      system(command);
      }
    } 
  }
                //line 90
int set()         
  {
  char buffer[256];
  ifstream settings ("Settings.txt");
  if (! settings.is_open())       
    {
    cout << "Error opening file"; exit (1);
    }
  while (! settings.eof() )
    {                     //line 100
    settings.getline (buffer,100);
    filename1 = buffer;
    settings.getline (buffer,100);
    filename2 = buffer;
    settings.getline (buffer,100);   
    time1 = str2int(buffer);
    settings.getline (buffer,100);
    time2 = str2int(buffer);
    }
  cout << filename1 << "\n" << filename2 << "\n" << time1 << "\n" << time2 << "\n";    //line 110
  }


here's the errors which make no sense at all to me...:
Code: Select all
c++ -DHAVE_CONFIG_H -I. -I. -I.. -O2 -O0 -g3 -Wall -fno-exceptions -fno-check-new -c main.cpp
main.cpp:29: error: 'string' is used as a type, but is not defined as a type.
main.cpp:34: error: 'string' is used as a type, but is not defined as a type.
main.cpp:35: error: 'string' is used as a type, but is not defined as a type.
main.cpp: In function `int main()':
main.cpp:77: error: `command' undeclared (first use this function)
main.cpp:77: error: (Each undeclared identifier is reported only once for each function it appears in.)
main.cpp:78: error: `filename1' undeclared (first use this function)
main.cpp:84: error: `filename2' undeclared (first use this function)
main.cpp:111:4: warning: no newline at end of file
gmake: *** [main.o] Error 1
*** failed ***
(22:24:12) Urza4189: I programmed like a motherfucking beast...I don't know how I did it...but I organized and commented like an 8 year old - not at all
User avatar
Urza9814
 
Posts: 185
Joined: Sun Nov 02, 2003 2:18 pm
Location: Indiana, PA

Postby Guest » Sun Aug 01, 2004 7:13 pm

go home wrote:That is obviously Dave Sinkula so go back to http://cboard.cprogramming.com/index.php? and stay there.
Last edited by Guest on Thu Jan 06, 2005 8:57 am, edited 1 time in total.
Guest
 

Postby Urza9814 » Sun Aug 01, 2004 7:28 pm

AURGH!
I need to use system() with a string...but it only works with a char...and I can't convert the string to a char because I have no idea how long it's gonna be...is there any way around this?
(22:24:12) Urza4189: I programmed like a motherfucking beast...I don't know how I did it...but I organized and commented like an 8 year old - not at all
User avatar
Urza9814
 
Posts: 185
Joined: Sun Nov 02, 2003 2:18 pm
Location: Indiana, PA


Return to Unix/Linux

Who is online

Users browsing this forum: No registered users and 3 guests