I have some fairly simple, or so i thought, code that i had to write for an example problem. The book i'm following is not very straightforward, and it really doesnt even have examples of how code should be written to work properly. it just basically defines functions and says what they do but not how to use them. It's a directed study so i don't really ever meet with my teacher. I could use some help on this topic and if someone could explain to me how to move data out of and back into files it could help me finish the rest of my programs before the due date. Which by the way is in three days from now. Source code that needs fixing is below. I am using visual studio 2010 for c++
Programmer : Maxwell Hill
Assignment : Final
Write a program that gives and takes advice on program writing. The program starts by writing a piece of advice on the screen and asking the user to type in a different
piece of advice. The progarm ends. The next person to run the program receives the advice given by the person who last ran the program. The advice is kept in a file
(name it advice.txt) and the contents of the file change after each run of the program. You can use a text editor (NotePad) to enter the initial piece of advice in the
file so that the first person who runs the program can recieve some advice. Allow the user to type in advice of any length so that it can be any number of lines long.
The user is told to end his or her advice by pressing the Enter key two times; you can handle that any way you like.
void Explanation()
{
cout << " PROGRAMMING ADVICE " << endl << endl;
}
void LastUser()
{
fstream document ("advice.txt");
document.open ("advice.txt", ios::in);
cout << "Here is the advice given by the last person who ran this program:" << endl << endl;
getline(document,Text,'#');
cout << Text <<endl << endl;
}
void CurrentUser()
{
fstream document;
document.open ("advice.txt", ios::out );
cout << "Please enter your advice below. Your advice may be as long as you" << endl;
cout << "need, but please indicate the end of input by pressing <Enter> twice:" << endl;
cin >> Text;
document << Text;
document.close();
}
#include<iostream>#include<string>#include<fstream>usingnamespacestd;voidExplanation();voidLastUser();voidCurrentUser();stringText="";intmain(){system("cls");Explanation();LastUser();CurrentUser();system("pause");return0;}voidExplanation(){cout<<" PROGRAMMING ADVICE "<<endl<<endl;}voidLastUser(){fstreamdocument("advice.txt");document.open("advice.txt",ios::in);cout<<"Here is the advice given by the last person who ran this program:"<<endl<<endl;getline(document,Text,'#');cout<<Text<<endl<<endl;}voidCurrentUser(){fstreamdocument;document.open("advice.txt",ios::out);cout<<"Please enter your advice below. Your advice may be as long as you"<<endl;cout<<"need, but please indicate the end of input by pressing <Enter> twice:"<<endl;cin>>Text;document<<Text;document.close();}
All I did there was clean the code up a bit so others could read it easier. You might have more of a chance with people helping that way. ;}
Here is some example code from one of my projects a long time ago. I read and write to a file in the defined functions. Hope this helps
/* CSCI 201 Computer Science 1 Spring, 2010 Project 10 March 31 My Name Section 1 This program opens a data file containing the item numbers, item names, item quanititys, and item unit prices. Next, it sorts the items by item number, then calculates the total value of each item, and the total value of all items. */#include<cstdlib>#include<iostream>#include<iomanip>#include<fstream>usingnamespacestd;/* Struct: item Description: Contains an items reference number, quantity in the warehouse, unit price, total value, and name.*/structitem{intnum,amount;floatprice,val;charname[15];};/* Function: open_file Description: Opens an item file containing unorganized warehouse item information. Input: item array, count variable of items in array Output: None*/voidopen_file(itemobj[],int&c);/* Function: arrange_items Description: Arranges the warehouse items by item number. Input: item array, count variable of items in array Output: None*/voidarrange_items(itemobj[],intc);/* Function: calc_val Description: Calculates each individal item's total value, and returns the total value of all items. Input: item array, count variable of items in array Output: Total value of all items in warehouse*/doublecalc_val(itemobj[],intc);/* Function: write_file Description: Writes the warehouse item information to a file with organized by item number, and containing the additional calculated value, and total value of all items. Input: item array, count variable of items in array, total value Output: None*/voidwrite_file(itemobj[],intc,doubletotal);intmain(){itemobjects[100];// Array of itemsintcount=0;// Number of items in arraydoubletotal=0;// Total value of all items in arrayopen_file(objects,count);arrange_items(objects,count);total=calc_val(objects,count);write_file(objects,count,total);return0;}voidopen_file(itemobj[],int&c){ifstreamin_f;// Input file streamin_f.open("struct.txt");// Target file to open// Reads in information to item arraywhile(!in_f.eof()){in_f>>obj[c].num>>obj[c].name>>obj[c].price>>obj[c].amount;c++;}in_f.close();}voidarrange_items(itemobj[],intc){itemtemp;// Temporary storage while ordering itemsfor(intn=c-1;n>0;n--){for(intk=0;k<n;++k)if(obj[k].num>obj[k+1].num){temp=obj[k];obj[k]=obj[k+1];obj[k+1]=temp;}}}doublecalc_val(itemobj[],intc){doubletotal=0;// Initialize total to 0for(intn=0;n<c;++n){obj[n].val=obj[n].amount*obj[n].price;// Calculate total price*amounttotal+=obj[n].val;// Sum to total}returntotal;}voidwrite_file(itemobj[],intc,doubletotal){ofstreamout_f;// Output file streamout_f.open("inventory.txt");// Output file nameout_f<<"Number Name Unit Price Quantity Value"<<endl<<endl;// Write item informationfor(intn=0;n<c;++n){out_f<<obj[n].num<<setw(16)<<obj[n].name<<setw(11)<<obj[n].price<<setw(11)<<obj[n].amount<<setw(12)<<obj[n].val<<endl;}out_f<<endl<<"Total Value = $"<<total;out_f.close();}
im looking at your code and it really made a difference, not sure where i stand right now, i'm trying to figure it out but i'll update this in probably an hour or two with wether or not i finsihed the program.
output is working now, it was really nice to have your stuff to look at. For some reason my string wont allow spaces in it, it just ends if there is a space. I don't understand why it is doing that.
// code::::
// Programmer : Maxwell Hill
// Assignment : Final
// Write a program that gives and takes advice on program writing. The program starts by writing a piece of advice on the screen and asking the user to type in a different
// piece of advice. The progarm ends. The next person to run the program receives the advice given by the person who last ran the program. The advice is kept in a file
// (name it advice.txt and the contents of the file change after each run of the program. You can use a text editor (NotePad) to enter the initial piece of advice in the
// file so that the first person who runs the program can recieve some advice. Allow the user to type in advice of any length so that it can be any number of lines long.
// The user is told to end his or her advice by pressing the Enter key two times; you can handle that any way you like.
ifstream document;
document.open ("advice.txt");
cout << "Here is the advice given by the last person who ran this program:" << endl << endl;
while (!document.eof())
{
getline(document,Text,'\n');
}
cout << Text <<endl;
}
void CurrentUser()
{
ofstream document;
document.open ("advice.txt",ios::out);
cout << "Please enter your advice below. Your advice may be as long as you" << endl;
cout << "need, but please indicate the end of input by pressing <Enter> twice:" << endl;
cin >> Text;
cout << Text;
document << Text;
You're using cin to read the advice from the console. Bad idea. Not only does cin suck but it also delimits automatically. cin will only read a single word.
You should use getline for that too:
voidCurrentUser(){ofstreamdocument;document.open("advice.txt",ios::out);cout<<"Please enter your advice below. Your advice may be as long as you"<<endl;cout<<"need, but please indicate the end of input by pressing <Enter> twice:"<<endl;stringadvice;//This saves you the global string variable. Globals aren't a good thing usually.//"ifstream document" is a stream. That's why we can extract information from it using getline.//Luckily cin is also a stream (where all the console input is written into). So we can use getline too!getline(cin,advice,'\n');cout<<endl<<"Your advice: "<<advice;document<<advice;//A bit more nicely formatted.document.close();}
Oh, another tip.
If you move the main() function below the other functions then you don't have to use the forward declarations.
It doesn't really matter much for a single-file project, but most programmers are used to reading programs bottom up. It's kind of a unwritten standard.
Unnecessary forward declarations just increase linking times a little and make the code more bloated.
Thanks dude, this book really doesn't explain much about how things work and since it's a directed study i meet with my teacher once a month if that. I have just started using files to store data and for some reason it seems like there is a lot more complexity than there needs to be. Maybe I am just slow or i'm missing something because everything up until this point worked very easily, almost without thinking.
By the way, that getline syntax was really nice. Other people were telling me to do like a document.cin([Large Number],Text,'\n') which didn't seem like a good idea to me.
Cheers, feel free to add +1 internets
Rollback Post to RevisionRollBack
To post a comment, please login or register a new account.
I have some fairly simple, or so i thought, code that i had to write for an example problem. The book i'm following is not very straightforward, and it really doesnt even have examples of how code should be written to work properly. it just basically defines functions and says what they do but not how to use them. It's a directed study so i don't really ever meet with my teacher. I could use some help on this topic and if someone could explain to me how to move data out of and back into files it could help me finish the rest of my programs before the due date. Which by the way is in three days from now. Source code that needs fixing is below. I am using visual studio 2010 for c++
Programmer : Maxwell Hill Assignment : Final Write a program that gives and takes advice on program writing. The program starts by writing a piece of advice on the screen and asking the user to type in a different piece of advice. The progarm ends. The next person to run the program receives the advice given by the person who last ran the program. The advice is kept in a file (name it advice.txt) and the contents of the file change after each run of the program. You can use a text editor (NotePad) to enter the initial piece of advice in the file so that the first person who runs the program can recieve some advice. Allow the user to type in advice of any length so that it can be any number of lines long. The user is told to end his or her advice by pressing the Enter key two times; you can handle that any way you like.
using namespace std;
void Explanation(); void LastUser(); void CurrentUser();
string Text = "";
int main() {
system("cls"); Explanation(); LastUser(); CurrentUser(); system("pause");
return(0); }
void Explanation() { cout << " PROGRAMMING ADVICE " << endl << endl; } void LastUser() { fstream document ("advice.txt"); document.open ("advice.txt", ios::in); cout << "Here is the advice given by the last person who ran this program:" << endl << endl; getline(document,Text,'#'); cout << Text <<endl << endl;
} void CurrentUser() { fstream document; document.open ("advice.txt", ios::out ); cout << "Please enter your advice below. Your advice may be as long as you" << endl; cout << "need, but please indicate the end of input by pressing <Enter> twice:" << endl; cin >> Text; document << Text; document.close(); }
All I did there was clean the code up a bit so others could read it easier. You might have more of a chance with people helping that way. ;}
Here is some example code from one of my projects a long time ago. I read and write to a file in the defined functions. Hope this helps
@iSaintx: Go Thanks :)
@RedFromWinter: Go
im looking at your code and it really made a difference, not sure where i stand right now, i'm trying to figure it out but i'll update this in probably an hour or two with wether or not i finsihed the program.
@happy04
output is working now, it was really nice to have your stuff to look at. For some reason my string wont allow spaces in it, it just ends if there is a space. I don't understand why it is doing that.
// code::::
// Programmer : Maxwell Hill
// Assignment : Final
// Write a program that gives and takes advice on program writing. The program starts by writing a piece of advice on the screen and asking the user to type in a different
// piece of advice. The progarm ends. The next person to run the program receives the advice given by the person who last ran the program. The advice is kept in a file
// (name it advice.txt and the contents of the file change after each run of the program. You can use a text editor (NotePad) to enter the initial piece of advice in the
// file so that the first person who runs the program can recieve some advice. Allow the user to type in advice of any length so that it can be any number of lines long.
// The user is told to end his or her advice by pressing the Enter key two times; you can handle that any way you like.
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
void Explanation();
void LastUser();
void CurrentUser();
string Text = "";
int main()
{
system("cls");
Explanation();
LastUser();
CurrentUser();
system("pause");
return(0);
}
void Explanation()
{
cout << " PROGRAMMING ADVICE " << endl << endl;
}
void LastUser()
{
ifstream document;
document.open ("advice.txt");
cout << "Here is the advice given by the last person who ran this program:" << endl << endl;
while (!document.eof())
{
getline(document,Text,'\n');
}
cout << Text <<endl;
document.close();
}
void CurrentUser()
{
ofstream document;
document.open ("advice.txt",ios::out);
cout << "Please enter your advice below. Your advice may be as long as you" << endl;
cout << "need, but please indicate the end of input by pressing <Enter> twice:" << endl;
cin >> Text;
cout << Text;
document << Text;
document.close();
}
@happy04: Go
You're using cin to read the advice from the console. Bad idea. Not only does cin suck but it also delimits automatically. cin will only read a single word.
You should use getline for that too:
Oh, another tip.
If you move the main() function below the other functions then you don't have to use the forward declarations.
It doesn't really matter much for a single-file project, but most programmers are used to reading programs bottom up. It's kind of a unwritten standard.
Unnecessary forward declarations just increase linking times a little and make the code more bloated.
@s3rius:
Thanks dude, this book really doesn't explain much about how things work and since it's a directed study i meet with my teacher once a month if that. I have just started using files to store data and for some reason it seems like there is a lot more complexity than there needs to be. Maybe I am just slow or i'm missing something because everything up until this point worked very easily, almost without thinking.
By the way, that getline syntax was really nice. Other people were telling me to do like a document.cin([Large Number],Text,'\n') which didn't seem like a good idea to me.
Cheers, feel free to add +1 internets