473,387 Members | 3,801 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

File I/O problem and other functions

11
Hey everyone,
I was making a program for a class, and I ran into a problem that is driving me crazy. I first suspected Vista, and since I am not home I cannot verify if it just my Vista computer, anyway, here is the problem.
I know this is a lame program, but I lacked the creativity to come up with a program including all the required items in three days. I ended up getting 351/585(60%) on this because, I didn't include 2 arrays, more user defined functions, and my File I/O didn't work.
The problem is; When you select option 6 in the switch statement it doesn't stop and let you input anything. It just says enter definition, and runs the list again. I tried to do this with JUST iFile >> other;, and that didn't work either. I then went back to my week 3 assignment which was using file I/O and it didn't work on this computer, but on my desktop at home it runs fine, or at least it did in week 3. Oh! It does create the new file though, but just won't accept any string input to be output INSIDE the file.
Previously, I had multiple if...else statements, instead of a switch. Well if I used cin.get(var, xx) it would only display a space and the last word, so instead of "Hi there" it would only output " there". The same exact thing happened with getline(cin, var). If it was anywhere after the if...else statements it would not output correctly. I had to re-arrange everything to get cin.get and getline to work properly. Does anyone know why?

So the problems are:
File I/O is not accepting string input.
getline() does not work after if/if...else statements no matter how far down after.
cin.get() does not work after if/if...else statements no matter how far down after.

Is it Vista or is it me, or both?
I think I downloaded the updates for Vista, so it may or may not be that.

Thanks in advance
Joshua

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <string>
  3. #include <fstream>
  4. #include <cstring>
  5.  
  6. using namespace std;
  7.  
  8. void welcome();
  9.  
  10. int main()
  11. {
  12.  
  13.  
  14.     char verify; //declare user choice to verify favorite song
  15.     char favSong[30]; //declare favorite song c-string
  16.     char misc[50]; 
  17.  
  18.     int choice; //declare user choice of genre
  19.  
  20.     string favBand; //declare string for name of favorite band
  21.     string other;
  22.  
  23.     ifstream iFile;
  24.     ofstream oFile;
  25.  
  26.     iFile.open("iDef.txt");
  27.     oFile.open("oDef.txt");
  28.  
  29.     welcome();    //A user defined function.
  30.     cout << "*******Welcome*******" << endl;
  31.     welcome();
  32.     cout << "**To Your Favorites**" << endl;
  33.     welcome ();
  34.  
  35.     cout << "Enter the name of your favorite band\n"; //prompt for favorite band
  36.     getline(cin, favBand); //getline command to accept full string input
  37.     cout << endl;
  38.  
  39.     cout << "Your favorite band is: " << favBand << endl; //output of user's input
  40.  
  41.     cout << "\nWhat is your favorite song of all time?" << endl; //second prompt; favorite song
  42.     cin.get(favSong, 30); //cin.get() command to accept whitespaces 
  43.  
  44.     cout << "Your favorite song of all time is: " << favSong << endl;//output of user's input
  45.  
  46.     cout << "\nIs this correct?" << "\nPress 'Y' for Yes or 'N' for No. "; //prompt confirming choices
  47.     cin >> verify;
  48.     cout << "Your choice is: " << verify << endl;
  49.  
  50.         //list of choices and input "choice" from user
  51.         cout << "\nHere is a list of definitions of different genre's. " << endl;
  52.         cout << "Check the definition and you may be surprised of the description's." << endl;
  53.     do
  54.     {
  55.         cout << "\nYour choices are:" << endl;
  56.         cout << "1. Rock 'n' roll " << endl;
  57.         cout << "2. R & B" << endl;
  58.         cout << "3. Rap" << endl;
  59.         cout << "4. Country" << endl;
  60.         cout << "5. Pop" << endl;
  61.         cout << "6. Other" << endl;
  62.         cout << "7. Quit" << endl;
  63.         cin >> choice; 
  64.         cout << endl;
  65.  
  66.         switch (choice)
  67.         {
  68.         case 1:
  69.             cout << "Your choice is Rock 'n' Roll. The definition of Rock 'n' Roll is: "  << endl;
  70.             cout << "rock 'n' roll: a genre of popular music originating in the 1950s;" << endl; 
  71.             cout << "a blend of Black rhythm-and-blues with White country-and-western; " << endl;
  72.             cout << "rock is a generic term for the range of styles that evolved out of rock'n'roll." << endl; 
  73.             break;
  74.         case 2:
  75.             cout << "Your choice is R & B. The definition of R  & B is: " << endl;
  76.             cout << "Rhythm and Blues (R&B)--a hybrid of traditional African-American folk " << endl;
  77.             cout << "roots and blues with various mainstream sources. R&B employs a wider variety " << endl;
  78.             cout << "of song structures and chord progressions than traditional blues; and, like " << endl;
  79.             cout << "jazz, it often features complex arrangements that may be unique to one " << endl;
  80.             cout << "particular song. Also, many R&B songs differ from traditional music in that " << endl;
  81.             cout << "they are created by professional songwriters with the calculated goals of " << endl;
  82.             cout << "scoring a hit and making money." << endl;
  83.             break;
  84.         case 3:
  85.             cout << "Your choice is Rap. The definition of Rap is: " << endl;
  86.             cout << "A form of rap music characterized by violent, often degrading lyrics." << endl;
  87.             cout << "Also known as Hip-Hop." << endl;
  88.             cout << "The cultural context of rap music found in the urban style of dress, speech and art." << endl;
  89.             break;
  90.         case 4:
  91.             cout << "Your choice is Country. The definition of Country is: " << endl;
  92.             cout << "rooted in British ballads and folk songs originally played on the fiddle in " << endl;
  93.             cout << "Britain were adapted to the banjo and guitar, which have African and Spanish " << endl;
  94.             cout << "origins, respectively. Commercial country has been aggressively marketed " << endl;
  95.             cout << "since the 1920s, developing new traditions within itself, such as the mid " << endl;
  96.             cout << "20th century honky-tonk style. As commercial country grows increasingly " << endl;
  97.             cout << "mainstream, however, there are periodic back-to-basics movements in which " << endl;
  98.             cout << "older styles are re-emphasized, including those with strong British roots." << endl;
  99.             break;
  100.         case 5:
  101.             cout << "Your choice is Pop. The definition of Pop is: " << endl;
  102.             cout << "music of general appeal to teenagers; a bland watered-down version of " << endl; 
  103.             cout << "rock'n'roll with more rhythm and harmony and an emphasis on romantic love." << endl;
  104.             break;
  105.         case 6:
  106.             cout << "Your choice is Other" << endl;
  107.             cout << "Give a description/definition of this genre please: " << endl;
  108.             oFile << "The Definition of other is:" << endl;
  109.             iFile >> other;
  110.             break;
  111.         case 7:
  112.             cout << "Quitting!" << endl;
  113.             break;
  114.         default:
  115.             cout << "Improper selection, exiting..." << endl;
  116.         }
  117.     }
  118.     while(choice != 7 && choice != 7);
  119.  
  120.     cout << endl;
  121.  
  122.     iFile.close();
  123.     oFile.close();
  124.  
  125.  
  126.     return 0;
  127.  
  128. }
  129.  
  130.  
  131. void welcome()//declaration of user defined function
  132. {
  133.     cout << "*********************" << endl;
  134.     cout << "*********************" << endl;
  135. }
  136.  
Oct 8 '07 #1
10 1873
RRick
463 Expert 256MB
Case 6 problem: For interactive io, you need to use cin, and not some input file stream. Cin is tied to the keyboard and will pause the program until you input some value. The iFile variable will read from the file and will not pause the program.
Oct 8 '07 #2
strife
11
Case 6 problem: For interactive io, you need to use cin, and not some input file stream. Cin is tied to the keyboard and will pause the program until you input some value. The iFile variable will read from the file and will not pause the program.
I need to input the string into the file though, cin >> will not do this will it?
Oct 8 '07 #3
RRick
463 Expert 256MB
It sounds like you have a 2 step problem. First, you use cin to get the value from the keyboard into the string. Then you put the string into an output file using oFile.

You can't just go from cin to oFile (at least, I've never seen it used this way).
Oct 8 '07 #4
strife
11
It sounds like you have a 2 step problem. First, you use cin to get the value from the keyboard into the string. Then you put the string into an output file using oFile.

You can't just go from cin to oFile (at least, I've never seen it used this way).
Well thats my problem. How do I get the string to be displayed in the file?

Also, another thing that is bugging me is when I run the File I/O by itself it still does not work. Can you not use a string to input something into a file? I.e.

Expand|Select|Wrap|Line Numbers
  1. ifstream inFile;
  2. ofstream outFile;
  3.  
  4. inFile.open("i.txt);
  5. outFile.open("o.txt");
  6.  
  7. inFile >> string;
  8. outFile << string << endl;
  9.  
  10. inFile.close();
  11. outFile.close();
  12.  
Something like this will not work all by itself either for some reason. Is it because you can ONLY name files with strings? This is what I am thinking. Is there a way to enter a string into a file?
Oct 8 '07 #5
strife
11
No one knows? I guess I will try another way.
Oct 9 '07 #6
strife
11
It sounds like you have a 2 step problem. First, you use cin to get the value from the keyboard into the string. Then you put the string into an output file using oFile.

You can't just go from cin to oFile (at least, I've never seen it used this way).
Yeah I tried that and it did not work. I just need to figure out a way to use File I/O another way I guess.

Thanks for the help
Oct 9 '07 #7
oler1s
671 Expert 512MB
I'm not sure how we can help you. We gave you the solution and you report it doesn't work. Unfortunately, in something technically precise like programming, "doesn't work" isn't very useful information. Does it not compile? Or runtime errors? Do little fairies stop the program from running? We certainly can't help.

Oh, and uh, it's probably your fault you can't get it working right. Nothing personal. You're learning, and you probably will want to see what went wrong with the code you wrote, rather than look for another method of file i/o.

Actually, I don't know what you mean by another method. Are you asking if there's a way to directly pipe from stdin to a file stream? I'm sure you can pipe console stdin to a file stream, but that's ridiciulously intensive work for something that can be handled with two or three lines of code.
Oct 9 '07 #8
RRick
463 Expert 256MB
In your post #5, your code will transfer the first single word from the input file to the output file. If you want to transfer the complete file you will have to use a loop.

Also, string causes the << and >> operators to parse on whitespace (i.e. space, tab, and '\n'). If you want to save the whitespace, you have to add it yourself or use other methods. For example, the getline function, reads a line at a time and saves the whitespace, but throws away the '\n'. There are other methods that read exactly what is in the file, whether it is text or binary.

The code below transfers one text file to another.
Expand|Select|Wrap|Line Numbers
  1. #include <fstream>
  2. using namespace std;
  3.  
  4.  
  5. int main( )
  6. {
  7.     ifstream inFile;
  8.         ofstream outFile;
  9.  
  10.         inFile.open("tt.cpp");
  11.         outFile.open("o.txt");
  12.  
  13.         string tmp;
  14.         for( ; getline( inFile, tmp); !inFile.eof())
  15.         {
  16.             outFile << tmp << endl;
  17.         }
  18.  
  19.         inFile.close();
  20.         outFile.close();
  21. }
Oct 9 '07 #9
Have you tried using the gets(<name>) option and then try writing it on to the file. Here name is a char[] type. or you can declare it as char *name also
Oct 11 '07 #10
On the other hand, I tried your program on a Dev CPP compiler and is just working fine except for the cin you have forgotten in option 6.
Oct 11 '07 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Bruce W...1 | last post by:
I'm a PHP newbie coming from experience with ASP.NET. I want to have a separate PHP file to support each HTML PHP page. This would be the equivalent of an ASP.NET code-behind file but using PHP....
7
by: A_StClaire_ | last post by:
hi, I'm working on a project spanning five .cpp files. each file was used to define a class. the first has my Main and an #include for each of the other files. problem is my third file...
5
by: Pete | last post by:
I having a problem reading all characters from a file. What I'm trying to do is open a file with "for now" a 32bit hex value 0x8FB4902F which I want to && with a mask 0xFF000000 then >> right...
3
by: Paminu | last post by:
I have made a lot of helping functions that I would like to have in a seperate file. I have tried putting them in a file "functions.c". I then have the file "myprogram.c" that contains some...
10
by: bienwell | last post by:
Hi, I have a question about file included in ASP.NET. I have a file that includes all the Sub functions (e.g FileFunct.vb). One of the functions in this file is : Sub TestFunct(ByVal...
7
by: Petr Jakes | last post by:
I have got names of functions stored in the file. For the simplicity expect one row only with two function names: printFoo, printFOO In my code I would like to define functions and then to read...
1
by: laredotornado | last post by:
Hi, I'm using PHP 4.4.4 on Apache 2 on Fedora Core 5. PHP was installed using Apache's apxs and the php library was installed to /usr/local/php. However, when I set my "error_reporting"...
2
by: Sebouh | last post by:
Hi all. I'm not sure if this is the right place to ask, but here it goes. I'm trying to create a file system using the File System in User Space (FUSE) kernel module. From what i have understood, i...
1
AdrianH
by: AdrianH | last post by:
Assumptions I am assuming that you know or are capable of looking up the functions I am to describe here and have some remedial understanding of C programming. FYI Although I have called this...
80
by: nicolas.sitbon | last post by:
Hi everybody, in a french C book, the author says that only {fgetc, getc, getchar, fgetwc, getwc, getwchar, fgets, gets, fgetws, getws, fputc, putc, putchar, fputwc, putwc, putwchar, fputs, puts,...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.