473,657 Members | 2,571 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

File I/O problem and other functions

11 New Member
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 1900
RRick
463 Recognized Expert Contributor
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 New Member
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 Recognized Expert Contributor
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 New Member
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 New Member
No one knows? I guess I will try another way.
Oct 9 '07 #6
strife
11 New Member
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 Recognized Expert Contributor
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 Recognized Expert Contributor
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
sksriharsha
31 New Member
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

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

Similar topics

7
5171
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. For one page to do a variety of different things I need to call different methods (or functions) in the PHP code file that is dedicated to this purpose. How do you do this? How do you call just one method? Thanks for your help.
7
12960
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 needs to access the class defined in my second file and I can't figure out how to work this right. if I use an #include in my third file, my Main gives me a compile-time class redefinition error. if I don't, the third file can't "see" the second
5
2921
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 shift 24 bits storing in result then printing the result. I thing a while or for loop is needed but I'm not quite sure how to go about it. How do I step through each character in this case and store it for use and passing to another function. ...
3
1339
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 structs and other primary functions. In my make file I then have: myprogram: myprogram.c functions.c gcc myprogram.c -o myprogram
10
24063
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 strInput As String) return (strInput & " test") End Sub
7
2123
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 function names from the file, so the functions can be executed in the order the function names are stored in a file. While trying to read the names from the file I am getting always "strings" and I am not able to execute them. I would like to...
1
6476
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" setting to be "E_ALL", notices are still not getting reported. The perms on my file are 664, with owner root and group root. The php.ini file is located at /usr/local/lib/php/php.ini. Any ideas why the setting does not seem to be having an effect? ...
2
5549
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 need to implement certain functions, like the read, write, and delete that should be referenced by the fuse_main function. Now whenever my program uses the standard C functions, it references the functions i created to manipulate the file in my file...
1
64094
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 article “How to Parse a File in C++”, we are actually mostly lexing a file which is the breaking down of a stream in to its component parts, disregarding the syntax that stream contains. Parsing is actually including the syntax in order to make...
80
6459
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, fputws} are guaranteed to set the end-of-file indicator when the end-of-file is reached, but in C99 standard, I find p 288 (ISO/IEC 9899:TC3 Committee Draft — Septermber 7, 2007 WG14/N1256) /* ============================= */ #include <stdio.h>...
0
8392
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8305
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8730
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8605
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7321
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6163
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5632
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4301
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1607
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.