Im having trouble clearing the strg variable in my code, i have tryed cout<<flush(); and <<endl but each time i run the function it concatenates the new data with the old.

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

using namespace std;
int main()
{
  static char str[12], strg[100];
  char ismapname[] = "mapname:", ismapdisc[] = "mapdisc:";
  
      char filename [ FILENAME_MAX ];
      sprintf(filename, "%d.txt", roomnum);
      
      ifstream file(filename );
  
do{
    File >> str;
  }
     while (strcmp (str,ismapname) != 0);

do{  
    File >> str;
     if (strcmp (str,ismapdisc) == 0) break;
    strcat (strg,str);
  }
     while (strcmp (str,ismapname) != 0);
  
    puts (strg);
    cout<<endl<<endl;
    File.close();
}

Recommended Answers

All 10 Replies

single step thru your code in your debugger and you will see your problem.

The piece of code i showed isnt exactly the code i am using but it displays the info im after ok, problem is im using this function to get data from multiple files one at a time that have the same lay out, when i run the code it display first file ok but then second file round it double displays
eg
1st
roomname hallway
2nd
roomname hallway roomname kitchen

What i was hoping is there was some way to reset the variables so when i read the next file it dont display the previous one again aswell.

Let me see if I understand what you are trying to do. You start with a C-style string variable set to roomname. You then concatenate hallway to that string to create roomname hallway on the first pass through. You then want to reset the string string variable to roomname so the second time through you end up with roomname kitchen instead of roomname hallway roomname kitchen or roomname hallway kitchen or whatever.

If that's correct then why not strcpy roomname to the string variable after completing the first pass through and before the second pass. Here's an example of what I expect to happen.

step 1:
char str[8] = "A";
cout << str; //output: A

step 2:
strcat(str, " house");
cout << str; //output: A house

step 3:
strcpy(str, "A");
cout << str; //output: A

step 4:
strcat(str, " car");
cout << str; //output: A car

If you were to output str char by char after step four I expect you would find this:

str[0] == 'A';
str[1] == ' ';
str[2] == 'c';
str[3] == 'a';
str[4] == 'r';
str[5] == '\0';
str[6] == 'e';
str[7] == '\0';

However, since << will stop at the first null char and as long as you don't try to access what's beyond the first null char explicitly it's as good as erased.

I don't have compiler at this time to confirm my code, sorry.

well i dont see a strcpy in your code. Anyway you could use memset to zero the array. look it up in your helpfiles.

I guess my point was that you don't have to flush the C string buffer, you can reset it to a default value (A in my example, and apparently roomname in the OP) and accomplish the same thing (for all practical purposes) as long as you don't try to read past the first null char in the char array/Cstyle string.

You can "clear" a C-style string by writing a null byte to the first character:

strg[0] = '\0';

Yup, that's another way to "flush" or "clear" a C style variable, but it doesn't reset the buffer to a given non-empty default string. Depending on the authors needs, memset, assign null char to index == 0, or strcpy default value could all be used to accomplish the task of "removing" unwanted/unneeded data from a C style buffer.

Sorry i am new to the forums and how to correctly ask a question, i appreciate the time you guys give to trying to help people like myself.
I thought maybe i should give you my whole code that way theres no need for any guessing at what im trying to do.

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

using namespace std;

const char ismapname[]="roomname:";
const char ismapdisc[]="roomdisc:";              
const char isexit[]="exits:";                    
const char isitem[]="items:";

int displayroom(int roomnum)
{
    static char str[12];
    static char str2[12];
    static char str3[12];
    static char strg[100];
    static char strg2[100];
    static char strg3[100];
    char num[12];
    
      char filename [ FILENAME_MAX ];
      sprintf(filename, "%d.txt", roomnum);
      ifstream File(filename );             
      if (File.bad()) cout<<"An Error has happend on opening the file";
     
     cout<<endl;
   do{
                File>>str;
                }
                while (strcmp (str,ismapname) != 0);
                
   do{
                File>>str;
                if (strcmp (str,ismapdisc) == 0) break;
                strcat (strg,str);
                strcat (strg," ");
                }
                while (strcmp (str,isexit) != 0);
                
                cout<<strg<<endl<<endl;
                
   do{
                File>>str2;
                if (strcmp (str2,isexit) == 0) break;
                strcat (strg2,str2);
                strcat (strg2," ");
                }
                while (strcmp (str2,isexit) != 0);
                cout<<strg2<<endl;
                strcat (strg3, "\nYou see Exits: ");            
   
   do{
                File>>str3;
                File>>num;
                if ((File.eof()) || (strcmp (str3 ,isitem)== 0)) break;
                strcat (strg3,str3);
                strcat (strg3," ");
                }
                while (!File.eof());
                cout<<strg3<<endl;
                
                File.close();
                
}


int roomsave(char roomname[50],char roomdisc[256])
{
      int roomnum = 1001;
      
       char filename [ FILENAME_MAX ];
      sprintf(filename, "%d.txt", roomnum);
      
      ofstream file(filename);
      if (file.bad()) cout<<"An Error has happend saving the file";
      file<<roomname<<endl<<endl<<roomdisc;
      file.close();
} 


int roomedit(int roomnum)
{
    char roomname[30];
    char roomdisc[256];
    cout<<"Room Name :";
    cin.getline(roomname, 30);
    cout<<"\nRoom Discription :";
    cin.getline(roomdisc, 256);
    roomsave(roomname, roomdisc);
}


int movement(int roomnum)
{
    
    displayroom(roomnum);
}

int main()
{
    string ch;
    int ct = 0;
    
    int roomnum;
    if (ct== 0) roomnum = 1000;
        ct++;
        
    displayroom(roomnum);
do{
        cin>>ch;
    // is there a simpler way to write these if statements?
    if (ch=="north" || ch=="east" || ch=="south" || ch=="west")
        {
        {
                   static char str[12];
                   char filename [FILENAME_MAX];
                   sprintf(filename, "%d.txt", roomnum);
                   ifstream file(filename);
        do{
                   file>>str;
                   if (file.eof())
                   {
                                   cout<<"\nNo exit that way!\n";
                                   break;
                                   }
          }
                   while (str!=ch);
                   if (str==ch)
                   {
                               file>>roomnum;
                               file.close();
                               displayroom(roomnum);
                               }
                   file.close();
                   
                   }        
                  
       
        }
//    if (ch=="edit")
//        {
//       roomedit(roomnum);
//        }  
    if (ch=="l" || ch=="look")
        {
        displayroom(roomnum);      
        }
//    if (ch=="back" || ch=="b")
//        {
//        roomnum--;
//        displayroom(roomnum);
//        }
        
  }
while (ch!="q" && ch!="quit");
    
    
    //system("PAUSE");
    return 0;
}

These are the 3 files i have setup to read from.

1000.txt

roomname:
First Road

roomdisc:
My First Room

exits:
north
1001

items:

1001.txt

roomname:
Second Road

roomdisc:
My Second Room

exits:
north
1002
south
1000

items:

1002.txt

roomname:
Third Road

roomdisc:
My Third Room

exits:
south
1001

items:

I want my code to be c++ not c style, if i have c style in it can you please point out for me.
Thanks for time

Thank's Dave

Another question
Should i be looking at classes or structs as a better way to handle the data i read and write to the files ?

hould i be looking at classes or structs as a better way to handle the data i read and write to the files ?

If you don't know that the difference between classes and structs is default access being private or public, use classes and pretend you are putting a feather in your cap.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.