file i/o help

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jul 2008
Posts: 111
Reputation: clutchkiller is an unknown quantity at this point 
Solved Threads: 1
clutchkiller's Avatar
clutchkiller clutchkiller is offline Offline
Junior Poster

file i/o help

 
0
  #1
Nov 29th, 2008
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

  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. }
>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!
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 392
Reputation: StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light 
Solved Threads: 72
StuXYZ StuXYZ is offline Offline
Posting Whiz

Re: file i/o help

 
0
  #2
Nov 29th, 2008
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
  1. case 4:
  2. {
  3. ofstream cList;
  4. // ..... stuff
  5. }
  6. break;
  7. case 5:

Also please put break; after each case.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 111
Reputation: clutchkiller is an unknown quantity at this point 
Solved Threads: 1
clutchkiller's Avatar
clutchkiller clutchkiller is offline Offline
Junior Poster

Re: file i/o help

 
1
  #3
Nov 29th, 2008
But didn't i do it how you just showed me in your example?

  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.
>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!
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 392
Reputation: StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light 
Solved Threads: 72
StuXYZ StuXYZ is offline Offline
Posting Whiz

Re: file i/o help

 
1
  #4
Nov 30th, 2008
Originally Posted by clutchkiller View Post
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
  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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 111
Reputation: clutchkiller is an unknown quantity at this point 
Solved Threads: 1
clutchkiller's Avatar
clutchkiller clutchkiller is offline Offline
Junior Poster

Re: file i/o help

 
0
  #5
Nov 30th, 2008
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!
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 320
Reputation: cikara21 is an unknown quantity at this point 
Solved Threads: 63
cikara21's Avatar
cikara21 cikara21 is offline Offline
Posting Whiz

Re: file i/o help

 
0
  #6
Dec 1st, 2008
  1. //...
  2. bool repeat=false;
  3. do{
  4. //...
  5. switch(cho){
  6. default:
  7. //...Bad input
  8. repeat=true;}
  9. }while(repeat);
.:-cikara21-:.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC