943,793 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 610
  • C++ RSS
Nov 29th, 2008
0

file i/o help

Expand Post »
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

c++ Syntax (Toggle Plain Text)
  1.  
  2. #include <iostream>
  3. #include <fstream>
  4. #include <windows.h>
  5. #include <string>
  6.  
  7. using namespace std;
  8.  
  9. string catEnter;
  10. string itEnter;
  11.  
  12.  
  13. int main()
  14. {
  15. SetConsoleTitle("ItemIzer");
  16.  
  17. cout<<"\n\n\n\n";
  18. cout<<"\t1: View Catagory"<<endl;
  19. cout<<"\t2: Append new item"<<endl;
  20. cout<<"\t3: Delete an item"<<endl;
  21. cout<<"\t4: Create Catagory"<<endl;
  22. cout<<"\t5: Delete Catagory"<<endl;
  23.  
  24. do
  25. {
  26. int cho;
  27. bool repeat(false);
  28. cout<<">>";
  29. cin>>cho;
  30.  
  31.  
  32. switch (cho)
  33. {
  34. case 1:
  35.  
  36. case 2:
  37.  
  38. case 3:
  39.  
  40. case 4:
  41. ofstream cList;
  42. cout<<"Catagory Name>>";
  43. cin>>catEnter;
  44. cList.open ("Catagories.txt", ios::app);
  45. cList<<catEnter;
  46. cList.close();
  47. cin.get();
  48. break;
  49.  
  50. case 5:
  51.  
  52. default:
  53. cout<<"Bad user input, please select a menu option";
  54. repeat = true;
  55. }
  56. }
  57. while (repeat);
  58. }
Similar Threads
Reputation Points: 23
Solved Threads: 3
Junior Poster
clutchkiller is offline Offline
181 posts
since Jul 2008
Nov 29th, 2008
0

Re: file i/o help

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
c++ Syntax (Toggle Plain Text)
  1. case 4:
  2. {
  3. ofstream cList;
  4. // ..... stuff
  5. }
  6. break;
  7. case 5:

Also please put break; after each case.
Reputation Points: 732
Solved Threads: 134
Practically a Master Poster
StuXYZ is offline Offline
659 posts
since Nov 2008
Nov 29th, 2008
1

Re: file i/o help

But didn't i do it how you just showed me in your example?

c++ Syntax (Toggle Plain Text)
  1.  
  2. case 4:
  3. ofstream cList;
  4. cout<<"Catagory Name>>";
  5. cin>>catEnter;
  6. cList.open ("Catagories.txt", ios::app);
  7. cList<<catEnter;
  8. cList.close();
  9. cin.get();
  10. 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.
Reputation Points: 23
Solved Threads: 3
Junior Poster
clutchkiller is offline Offline
181 posts
since Jul 2008
Nov 30th, 2008
1

Re: file i/o help

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!
Ok, well you have gone up in my estimation. So let me see what I can do to answer the question. [feel free to ask for clarifications/questions]

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)
  1. // This code will fail to compile
  2. goto label;
  3. int a=4; // NOT OK.
  4. int x; // OK
  5. label:
  6. int b=10;
  7. // .. 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.
Reputation Points: 732
Solved Threads: 134
Practically a Master Poster
StuXYZ is offline Offline
659 posts
since Nov 2008
Nov 30th, 2008
0

Re: file i/o help

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!
Reputation Points: 23
Solved Threads: 3
Junior Poster
clutchkiller is offline Offline
181 posts
since Jul 2008
Dec 1st, 2008
0

Re: file i/o help

C++ Syntax (Toggle Plain Text)
  1. //...
  2. bool repeat=false;
  3. do{
  4. //...
  5. switch(cho){
  6. default:
  7. //...Bad input
  8. repeat=true;}
  9. }while(repeat);
Reputation Points: 47
Solved Threads: 69
Posting Whiz
cikara21 is offline Offline
340 posts
since Jul 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: building and creating the c++ dll wrapper
Next Thread in C++ Forum Timeline: error C3861: 'getPath': identifier not found





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC