| | |
file i/o help
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
Hi guys, im still learning some programming, and am confused on the subject of file i/o. I am getting this error but cant figure out why. Any help is appreciated, thanks
Keep in mind i have obviously not finished the case scenarios.
In function `int main()':
error: jump to case label
error: crosses initialization of `std::ofstream cList'
error: jump to case label
error: crosses initialization of `std::ofstream cList'
error: `repeat' undeclared (first use this function)
error: (Each undeclared identifier is reported only once for each function it appears in.)
warning: destructor needed for `cList'
warning: where case label appears here
warning: (enclose actions of previous case statements requiring destructors in their own scope.)
warning: destructor needed for `cList'
warning: where case label appears here
Execution terminated
Keep in mind i have obviously not finished the case scenarios.
In function `int main()':
error: jump to case label
error: crosses initialization of `std::ofstream cList'
error: jump to case label
error: crosses initialization of `std::ofstream cList'
error: `repeat' undeclared (first use this function)
error: (Each undeclared identifier is reported only once for each function it appears in.)
warning: destructor needed for `cList'
warning: where case label appears here
warning: (enclose actions of previous case statements requiring destructors in their own scope.)
warning: destructor needed for `cList'
warning: where case label appears here
Execution terminated
c++ Syntax (Toggle Plain Text)
#include <iostream> #include <fstream> #include <windows.h> #include <string> using namespace std; string catEnter; string itEnter; int main() { SetConsoleTitle("ItemIzer"); cout<<"\n\n\n\n"; cout<<"\t1: View Catagory"<<endl; cout<<"\t2: Append new item"<<endl; cout<<"\t3: Delete an item"<<endl; cout<<"\t4: Create Catagory"<<endl; cout<<"\t5: Delete Catagory"<<endl; do { int cho; bool repeat(false); cout<<">>"; cin>>cho; switch (cho) { case 1: case 2: case 3: case 4: ofstream cList; cout<<"Catagory Name>>"; cin>>catEnter; cList.open ("Catagories.txt", ios::app); cList<<catEnter; cList.close(); cin.get(); break; case 5: default: cout<<"Bad user input, please select a menu option"; repeat = true; } } while (repeat); }
>That confuses me =(
Get used to it. Good programmers are in a constant state of confusion.
>Looks like i have some quasi specifics to investigate! Up Up And AWAY!
Get used to it. Good programmers are in a constant state of confusion.
>Looks like i have some quasi specifics to investigate! Up Up And AWAY!
•
•
Join Date: Nov 2008
Posts: 392
Reputation:
Solved Threads: 72
As has been repeated many times in the last few days.
You cannot declare anything in the outside scope of a case statement.
you need either to put cList outside of the switch or use something like this
Also please put break; after each case.
You cannot declare anything in the outside scope of a case statement.
you need either to put cList outside of the switch or use something like this
c++ Syntax (Toggle Plain Text)
case 4: { ofstream cList; // ..... stuff } break; case 5:
Also please put break; after each case.
But didn't i do it how you just showed me in your example?
Sorry for not fully understanding what is going on, im am still in the current progress of trying to comprehend fstream and how to properly use it.
Edit: Ok, i see what you did, using { and }. But can you inform me why that worked now? I want to be able to understand what im writing instead of copy and pasting. Thanks!
c++ Syntax (Toggle Plain Text)
case 4: ofstream cList; cout<<"Catagory Name>>"; cin>>catEnter; cList.open ("Catagories.txt", ios::app); cList<<catEnter; cList.close(); cin.get(); break;
Sorry for not fully understanding what is going on, im am still in the current progress of trying to comprehend fstream and how to properly use it.
Edit: Ok, i see what you did, using { and }. But can you inform me why that worked now? I want to be able to understand what im writing instead of copy and pasting. Thanks!
Last edited by clutchkiller; Nov 29th, 2008 at 5:21 pm.
>That confuses me =(
Get used to it. Good programmers are in a constant state of confusion.
>Looks like i have some quasi specifics to investigate! Up Up And AWAY!
Get used to it. Good programmers are in a constant state of confusion.
>Looks like i have some quasi specifics to investigate! Up Up And AWAY!
•
•
Join Date: Nov 2008
Posts: 392
Reputation:
Solved Threads: 72
•
•
•
•
But can you inform me why that worked now? I want to be able to understand what im writing instead of copy and pasting. Thanks!
The C++ standard says that you cannot jump over an initialization statement (implicit or explicit). [ISO C++ 2003 6.7]
Interestingly, this means you cannot write
c++ Syntax (Toggle Plain Text)
// This code will fail to compile goto label; int a=4; // NOT OK. int x; // OK label: int b=10; // .. more stuff
The switch statement is such a code block (effectively). As I understand the reason, it is because the case points are actually effective labels. The worst example of that I have seen is Duff's device. It is on line everywhere and it is in the C++ book (as a question!!). [I haven't got my copy here so I can't remember which chapter it is one of the early ones]. But is uses a do { } while block with case statements within the do while!.
This is a start on a long road of strangeness. C++ has a strong tension between compiler writers / low-level access / high order coding. It is places like this that it comes to the fore.
Thank you for helping me out, the explanation is a little cloudy to me simply because of the vocabulary that i am still slightly unfamiliar with, but i think i can decipher it. Thanks!
>That confuses me =(
Get used to it. Good programmers are in a constant state of confusion.
>Looks like i have some quasi specifics to investigate! Up Up And AWAY!
Get used to it. Good programmers are in a constant state of confusion.
>Looks like i have some quasi specifics to investigate! Up Up And AWAY!
C++ Syntax (Toggle Plain Text)
//... bool repeat=false; do{ //... switch(cho){ default: //...Bad input repeat=true;} }while(repeat);
![]() |
Similar Threads
- connect to text file database (Visual Basic 4 / 5 / 6)
- Importing SQL Script File - Urgent !! (Database Design)
- Syntax for deleting specified file ( in C++) (C++)
- enabling file sharing (Windows NT / 2000 / XP)
- data file help (C)
- 81TB File Server (Networking Hardware Configuration)
Other Threads in the C++ Forum
- Previous Thread: building and creating the c++ dll wrapper
- Next Thread: error C3861: 'getPath': identifier not found
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray encryption error file forms fstream function functions game getline givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news node output parameter pointer problem program programming project proxy python read recursion recursive reference return rpg string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets





