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 -
#include <iostream>
-
#include <string>
-
#include <fstream>
-
#include <cstring>
-
-
using namespace std;
-
-
void welcome();
-
-
int main()
-
{
-
-
-
char verify; //declare user choice to verify favorite song
-
char favSong[30]; //declare favorite song c-string
-
char misc[50];
-
-
int choice; //declare user choice of genre
-
-
string favBand; //declare string for name of favorite band
-
string other;
-
-
ifstream iFile;
-
ofstream oFile;
-
-
iFile.open("iDef.txt");
-
oFile.open("oDef.txt");
-
-
welcome(); //A user defined function.
-
cout << "*******Welcome*******" << endl;
-
welcome();
-
cout << "**To Your Favorites**" << endl;
-
welcome ();
-
-
cout << "Enter the name of your favorite band\n"; //prompt for favorite band
-
getline(cin, favBand); //getline command to accept full string input
-
cout << endl;
-
-
cout << "Your favorite band is: " << favBand << endl; //output of user's input
-
-
cout << "\nWhat is your favorite song of all time?" << endl; //second prompt; favorite song
-
cin.get(favSong, 30); //cin.get() command to accept whitespaces
-
-
cout << "Your favorite song of all time is: " << favSong << endl;//output of user's input
-
-
cout << "\nIs this correct?" << "\nPress 'Y' for Yes or 'N' for No. "; //prompt confirming choices
-
cin >> verify;
-
cout << "Your choice is: " << verify << endl;
-
-
//list of choices and input "choice" from user
-
cout << "\nHere is a list of definitions of different genre's. " << endl;
-
cout << "Check the definition and you may be surprised of the description's." << endl;
-
do
-
{
-
cout << "\nYour choices are:" << endl;
-
cout << "1. Rock 'n' roll " << endl;
-
cout << "2. R & B" << endl;
-
cout << "3. Rap" << endl;
-
cout << "4. Country" << endl;
-
cout << "5. Pop" << endl;
-
cout << "6. Other" << endl;
-
cout << "7. Quit" << endl;
-
cin >> choice;
-
cout << endl;
-
-
switch (choice)
-
{
-
case 1:
-
cout << "Your choice is Rock 'n' Roll. The definition of Rock 'n' Roll is: " << endl;
-
cout << "rock 'n' roll: a genre of popular music originating in the 1950s;" << endl;
-
cout << "a blend of Black rhythm-and-blues with White country-and-western; " << endl;
-
cout << "rock is a generic term for the range of styles that evolved out of rock'n'roll." << endl;
-
break;
-
case 2:
-
cout << "Your choice is R & B. The definition of R & B is: " << endl;
-
cout << "Rhythm and Blues (R&B)--a hybrid of traditional African-American folk " << endl;
-
cout << "roots and blues with various mainstream sources. R&B employs a wider variety " << endl;
-
cout << "of song structures and chord progressions than traditional blues; and, like " << endl;
-
cout << "jazz, it often features complex arrangements that may be unique to one " << endl;
-
cout << "particular song. Also, many R&B songs differ from traditional music in that " << endl;
-
cout << "they are created by professional songwriters with the calculated goals of " << endl;
-
cout << "scoring a hit and making money." << endl;
-
break;
-
case 3:
-
cout << "Your choice is Rap. The definition of Rap is: " << endl;
-
cout << "A form of rap music characterized by violent, often degrading lyrics." << endl;
-
cout << "Also known as Hip-Hop." << endl;
-
cout << "The cultural context of rap music found in the urban style of dress, speech and art." << endl;
-
break;
-
case 4:
-
cout << "Your choice is Country. The definition of Country is: " << endl;
-
cout << "rooted in British ballads and folk songs originally played on the fiddle in " << endl;
-
cout << "Britain were adapted to the banjo and guitar, which have African and Spanish " << endl;
-
cout << "origins, respectively. Commercial country has been aggressively marketed " << endl;
-
cout << "since the 1920s, developing new traditions within itself, such as the mid " << endl;
-
cout << "20th century honky-tonk style. As commercial country grows increasingly " << endl;
-
cout << "mainstream, however, there are periodic back-to-basics movements in which " << endl;
-
cout << "older styles are re-emphasized, including those with strong British roots." << endl;
-
break;
-
case 5:
-
cout << "Your choice is Pop. The definition of Pop is: " << endl;
-
cout << "music of general appeal to teenagers; a bland watered-down version of " << endl;
-
cout << "rock'n'roll with more rhythm and harmony and an emphasis on romantic love." << endl;
-
break;
-
case 6:
-
cout << "Your choice is Other" << endl;
-
cout << "Give a description/definition of this genre please: " << endl;
-
oFile << "The Definition of other is:" << endl;
-
iFile >> other;
-
break;
-
case 7:
-
cout << "Quitting!" << endl;
-
break;
-
default:
-
cout << "Improper selection, exiting..." << endl;
-
}
-
}
-
while(choice != 7 && choice != 7);
-
-
cout << endl;
-
-
iFile.close();
-
oFile.close();
-
-
-
return 0;
-
-
}
-
-
-
void welcome()//declaration of user defined function
-
{
-
cout << "*********************" << endl;
-
cout << "*********************" << endl;
-
}
-
10 1873
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.
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?
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).
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. -
ifstream inFile;
-
ofstream outFile;
-
-
inFile.open("i.txt);
-
outFile.open("o.txt");
-
-
inFile >> string;
-
outFile << string << endl;
-
-
inFile.close();
-
outFile.close();
-
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?
No one knows? I guess I will try another way.
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
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.
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. -
#include <fstream>
-
using namespace std;
-
-
-
int main( )
-
{
-
ifstream inFile;
-
ofstream outFile;
-
-
inFile.open("tt.cpp");
-
outFile.open("o.txt");
-
-
string tmp;
-
for( ; getline( inFile, tmp); !inFile.eof())
-
{
-
outFile << tmp << endl;
-
}
-
-
inFile.close();
-
outFile.close();
-
}
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
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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....
|
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...
|
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...
|
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...
|
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...
|
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...
|
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"...
|
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...
|
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...
|
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,...
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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,...
|
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...
| |