help

This is a discussion on help within the C++ Programming forums, part of the General Programming Boards category; I have bin reading thou the tutorials and making this program to help my learning but recently I have tried ...

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    29

    help

    I have bin reading thou the tutorials and making this program to help my learning but recently I have tried to output name to a TXT file and after i added ofstream a_file ( "name.txt" ); and
    a_file<< string <<""; to main() it wont compile. here is my code:

    Code:
    #include <iostream>
    #include <fstream>
    
    void inerface1()
    	{
    	cout<<"__________________________________________________________________\n"; 
    	cout<<"|                         _______________                         |\n";
    	cout<<"|                        |               |                        |\n";
    	cout<<"|                        | input password|                        |\n";
    	cout<<"|                        | to continue   |                        |\n";
    	cout<<"|                        |_______________|                        |\n";
    	cout<<"|                                                                 |\n";
    	cout<<"|_________________________________________________________________|\n";
    	}
    
    void haltpro() //handles errors
    {
    	system("clear");
    	cout<<"__________________________________________________________________\n"; 
    	cout<<"|                         _______________                         |\n";
    	cout<<"|                        |               |                        |\n";
    	cout<<"|                        | error:        |                        |\n";
    	cout<<"|                        | terminating   |                        |\n";
    	cout<<"|                        |_______________|                        |\n";
    	cout<<"|                                                                 |\n";
    	cout<<"|_________________________________________________________________|\n";
    	cin.get();
    	}
    
    void menu() 
    	{
    	system("clear");
    	cout<<"__________________________________________________________________\n"; 
    	cout<<"|1. input and store age                                           |\n";
    	cout<<"|2. read age                                                      |\n";
    	cout<<"|3. store name                                                    |\n";
    	cout<<"|4. read name                                                     |\n";
    	cout<<"|5. store phone number                                            |\n";
    	cout<<"|6. read phone number                                             |\n";
    	cout<<"|_________________________________________________________________|\n";
    	}
    
    void startornot()
    	{
    	int a;
    	cin>>a;
    	if (a == 9999)
    		{
    		menu();
    		}
    	else
    		{
    		haltpro();
    		}
    		}
    
    void storename() 
    	{
    	system("clear");
    	cout<<"__________________________________________________________________\n"; 
    	cout<<"|                           ____________                          |\n";
    	cout<<"|                          |            |                         |\n";
    	cout<<"|                          | input name |                         |\n";
    	cout<<"|                          |____________|                         |\n";
    	cout<<"|                                                                 |\n";
    	cout<<"|                                                                 |\n";
    	cout<<"|_________________________________________________________________|\n";
    	cout<<"name:";
    	}
    
    void storephone()
    {
    	system("clear");
    	cout<<"__________________________________________________________________\n"; 
    	cout<<"|                           ____________                          |\n";
    	cout<<"|                          |            |                         |\n";
    	cout<<"|                          |input phone#|                         |\n";
    	cout<<"|                          |____________|                         |\n";
    	cout<<"|                                                                 |\n";
    	cout<<"|                                                                 |\n";
    	cout<<"|_________________________________________________________________|\n";
    	cout<<"phone #:";
    }
    
    void storeage()
    	{
    	system("clear");
    	cout<<"__________________________________________________________________\n"; 
    	cout<<"|                           ____________                          |\n";
    	cout<<"|                          |            |                         |\n";
    	cout<<"|                          | input age  |                         |\n";
    	cout<<"|                          |____________|                         |\n";
    	cout<<"|                                                                 |\n";
    	cout<<"|                                                                 |\n";
    	cout<<"|_________________________________________________________________|\n";
    	cout<<"age:";
    	}
    
    int main() 
    	{
    	system("clear");
    	inerface1();
    	startornot();
    	int z;
    	z = 1;
    	for(;;) {
    		int b;
    		cin>>b;
    		switch( b ) //desides on menu functions
    			{
    			case 1:
    			storeage();
    			int age;
    			cin>>age;
    			menu();
    			break;
    			case 2:
    			system("clear");
    			cout<<"__________________________________________________________________\n"; 
    			cout<<"|                           _____________                         |\n";
    			cout<<"|                          |             |                        |\n";
    			cout<<"|                          | inputed age |                        |\n";
    			cout<<"|                          |      "<< age <<"\n";
    			cout<<"|                          |_____________|                        |\n";
    			cout<<"|                                                                 |\n";
    			cout<<"|_________________________________________________________________|\n";
    			cout<<"press 9 to return:";
    			cin.get();
    			break;
    			case 3:
    			storename();
    			char string[50];
    			cin>> string, 50, '\n';
                            ofstream a_file ( "name.txt" );
                            a_file<< string <<"";
    			menu();
    			break;
    			case 4:
    			system("clear");
    			cout<<"__________________________________________________________________\n"; 
    			cout<<"|                           _____________                         |\n";
    			cout<<"|                          |             |                        |\n";
    			cout<<"|                          | stored name |                        |\n";
    			cout<<"|                          |"<< string <<"\n";
    			cout<<"|                          |_____________|                        |\n";
    			cout<<"|                                                                 |\n";
    			cout<<"|_________________________________________________________________|\n";
    			cout<<"press 9 to return:";
    			cin.get();
    			break;
    			case 5:
    			storephone();
    			int phone;
    			cin>> phone;
    			menu();
    			break;
    			case 6:
    			system("clear");
    			cout<<"__________________________________________________________________\n"; 
    			cout<<"|                          ______________                         |\n";
    			cout<<"|                         |              |                        |\n";
    			cout<<"|                         |inputed phone#|                        |\n";
    			cout<<"|                         |      "<< phone <<"\n";
    			cout<<"|                         |______________|                        |\n";
    			cout<<"|                                                                 |\n";
    			cout<<"|_________________________________________________________________|\n";
    			cout<<"press 9 to return:";
    			cin.get();
    			break;
    			case 9:
    			menu();
    			break;
    			default:
    			menu();
    			break;
    			}
    		}
    	}
    im a total noob to C++ so I'm most likely making a making a stupid mistake.
    Last edited by IM back!; 09-19-2007 at 06:11 PM.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    What compiler errors did you get?

    Note that your functions specify an int return value but don't return anything. That's allowed for main, but for the other functions you should change the return value to void.

    Also, could you edit your post and make the 9th line of your code into two lines so it doesn't stretch the page?

    Finally, proper indentation makes code much more readable. I don't know if the indentation was messed up when you posted the code here or if it looks like that when you code it, but it would make things easier on you and us if you could fix the indentation.

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    29
    Quote Originally Posted by Daved View Post
    What compiler errors did you get?

    Note that your functions specify an int return value but don't return anything. That's allowed for main, but for the other functions you should change the return value to void.

    Also, could you edit your post and make the 9th line of your code into two lines so it doesn't stretch the page?

    Finally, proper indentation makes code much more readable. I don't know if the indentation was messed up when you posted the code here or if it looks like that when you code it, but it would make things easier on you and us if you could fix the indentation.
    here are my compiler errors:
    /home/philipp/simple2/src/simple2.cpp: In function ‘int main()’:
    /home/philipp/simple2/src/simple2.cpp:140: error: jump to case label
    /home/philipp/simple2/src/simple2.cpp:136: error: crosses initialization of ‘std:fstream a_file’
    /home/philipp/simple2/src/simple2.cpp:153: error: jump to case label
    /home/philipp/simple2/src/simple2.cpp:136: error: crosses initialization of ‘std:fstream a_file’
    /home/philipp/simple2/src/simple2.cpp:159: error: jump to case label
    /home/philipp/simple2/src/simple2.cpp:136: error: crosses initialization of ‘std:fstream a_file’
    /home/philipp/simple2/src/simple2.cpp:172: error: jump to case label
    /home/philipp/simple2/src/simple2.cpp:136: error: crosses initialization of ‘std:fstream a_file’
    /home/philipp/simple2/src/simple2.cpp:175: error: jump to case label
    /home/philipp/simple2/src/simple2.cpp:136: error: crosses initialization of ‘std:fstream a_file’
    gmake: *** [simple2.o] Error 1
    gmake: Target `simple2' not remade because of errors.
    *** Exited with status: 2 ***
    BTW: it compiles and runs if i remove:
    ofstream a_file ( "name.txt" );
    a_file<< string <<""';

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,801
    Code:
    cin>> string, 50, '\n';
    Is that supposed to be a getline call?
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    Registered User
    Join Date
    Dec 2005
    Location
    Canada
    Posts
    267
    add braces for each case label

    Code:
    switch(...)
    {
        case 1:
        {
            //your code here
            break;
        }
    }

    OS: Windows 7, XUbuntu 11.10, Arch Linux
    IDE: CodeBlocks
    Compiler: GCC

  6. #6
    Registered User
    Join Date
    Sep 2007
    Posts
    29
    Quote Originally Posted by h_howee View Post
    add braces for each case label

    Code:
    switch(...)
    {
        case 1:
        {
            //your code here
            break;
        }
    }
    you dont need them.

  7. #7
    Registered User
    Join Date
    Dec 2005
    Location
    Canada
    Posts
    267
    your error code is telling me that you do
    basically what's happening here is you're skipping the variable's initialization with the case jumps so when the time comes to use the variables declared within the switch statement, they won't exist. You need the braces to prevent such errors from happening. Braces limit the variable's scope to between the opening braces and closing braces

    OS: Windows 7, XUbuntu 11.10, Arch Linux
    IDE: CodeBlocks
    Compiler: GCC

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, adding braces around each case would solve the problem. The error is essentially to do with your lines of code that says:
    Code:
                            ofstream a_file ( "name.txt" );
                            a_file<< string <<"";
    a_file is only in this single case, which means that you should use braces to tell the compiler that it's local to this case.

    This is confusing the compiler a little bit.

    I would actually prefer to make each case-statement much shorter, making each case-statement a single function call, to a function that does all the stuff you have in that case.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Registered User
    Join Date
    Sep 2007
    Posts
    29

    Unhappy

    I've changed some things around in my program to make it easier to work with. but now it dosn't compile (with ofstream a_file ( "name.txt" ); and a_file<< string <<""; removed) what am i doing wrong now?

    Code:
    #include <iostream>
    #include <fstream>
    
    using namespace std;
    
    int age;
    int phone;
    char string[50];
    
    void inerface1() //inteface for asking for password 
    {
    	cout<<"__________________________________________________________________\n"; 
    	cout<<"|                         _______________                         |\n";
    	cout<<"|                        |               |                        |\n";
    	cout<<"|                        | input password|                        |\n";
    	cout<<"|                        | to continue   |                        |\n";
    	cout<<"|                        |_______________|                        |\n";
    	cout<<"|                                                                 |\n";
    	cout<<"|_________________________________________________________________|\n";
    }
    
    void haltpro() //handles errors
    {
    	system("clear");
    	cout<<"__________________________________________________________________\n"; 
    	cout<<"|                         _______________                         |\n";
    	cout<<"|                        |               |                        |\n";
    	cout<<"|                        | error:        |                        |\n";
    	cout<<"|                        | terminating   |                        |\n";
    	cout<<"|                        |_______________|                        |\n";
    	cout<<"|                                                                 |\n";
    	cout<<"|_________________________________________________________________|\n";
    	cin.get();
    }
    
    void menu() //prints a menu
    {
    	system("clear");
    	cout<<"__________________________________________________________________\n"; 
    	cout<<"|1. input and store age                                           |\n";
    	cout<<"|2. read age                                                      |\n";
    	cout<<"|3. store name                                                    |\n";
    	cout<<"|4. read name                                                     |\n";
    	cout<<"|5. store phone number                                            |\n";
    	cout<<"|6. read phone number                                             |\n";
    	cout<<"|_________________________________________________________________|\n";
    }
    
    void startornot() //decides if password is right or not
    {
    	int a;
    	cin>>a;
    	if (a == 9999)
    	{
    		menu();
    	}
    	else
    	{
    		haltpro();
    	}
    }
    
    void storename() //renders name qestion
    {
    	system("clear");
    	cout<<"__________________________________________________________________\n"; 
    	cout<<"|                           ____________                          |\n";
    	cout<<"|                          |            |                         |\n";
    	cout<<"|                          | input name |                         |\n";
    	cout<<"|                          |____________|                         |\n";
    	cout<<"|                                                                 |\n";
    	cout<<"|                                                                 |\n";
    	cout<<"|_________________________________________________________________|\n";
    	cout<<"name:";
    	cin.getline ( string, 50, "/n");
    	menu();
    }
    
    void storephone() //renders name qestion
    {
    	system("clear");
    	cout<<"__________________________________________________________________\n"; 
    	cout<<"|                           ____________                          |\n";
    	cout<<"|                          |            |                         |\n";
    	cout<<"|                          |input phone#|                         |\n";
    	cout<<"|                          |____________|                         |\n";
    	cout<<"|                                                                 |\n";
    	cout<<"|                                                                 |\n";
    	cout<<"|_________________________________________________________________|\n";
    	cout<<"phone #:";
    	cin>> phone;
    	menu();
    }
    
    void storeage() //renders age qestion
    {
    	system("clear");
    	cout<<"__________________________________________________________________\n"; 
    	cout<<"|                           ____________                          |\n";
    	cout<<"|                          |            |                         |\n";
    	cout<<"|                          | input age  |                         |\n";
    	cout<<"|                          |____________|                         |\n";
    	cout<<"|                                                                 |\n";
    	cout<<"|                                                                 |\n";
    	cout<<"|_________________________________________________________________|\n";
    	cout<<"age:";
    	cin>>age;
    	menu();
    }
    	
    void restoreage() //renders age
    {
    	system("clear");
    	cout<<"__________________________________________________________________\n"; 
    	cout<<"|                           _____________                         |\n";
    	cout<<"|                          |             |                        |\n";
    	cout<<"|                          | inputed age |                        |\n";
    	cout<<"|                          |      "<< age <<"\n";
    	cout<<"|                          |_____________|                        |\n";
    	cout<<"|                                                                 |\n";
    	cout<<"|_________________________________________________________________|\n";
    	cout<<"press 9 to return:";
    	cin.get();
    }
    
    void restorename()
    {
    	system("clear");
    	char string[50];
    	cout<<"__________________________________________________________________\n"; 
    	cout<<"|                           _____________                         |\n";
    	cout<<"|                          |             |                        |\n";
    	cout<<"|                          | stored name |                        |\n";
    	cout<<"|                          |"<< string <<"\n";
    	cout<<"|                          |_____________|                        |\n";
    	cout<<"|                                                                 |\n";
    	cout<<"|_________________________________________________________________|\n";
    	cout<<"press 9 to return:";
    	cin.get();
    }
    
    void restorephone()
    {
    	system("clear");
    	cout<<"__________________________________________________________________\n"; 
    	cout<<"|                          ______________                         |\n";
    	cout<<"|                         |              |                        |\n";
    	cout<<"|                         |inputed phone#|                        |\n";
    	cout<<"|                         |      "<< phone <<"\n";
    	cout<<"|                         |______________|                        |\n";
    	cout<<"|                                                                 |\n";
    	cout<<"|_________________________________________________________________|\n";
    	cout<<"press 9 to return:";
    	cin.get();
    }
    			
    int main() //cal all functions
    {
    	system("clear");
    	inerface1();
    	startornot();
    	int z;
    	z = 1;
    	for(;;) {
    		int b;
    		cin>>b;
    		switch( b ) //desides on menu functions
    			{
    			case 1:
    			{
    				storeage();
    				break;
    			}
    			case 2:
    			{
    				restoreage();
    				break;
    			}
    			case 3:
    			{
    				storename();
    				break;
    			}
    			case 4:
    			{
    				restorename();
    				break;
    			}
    			case 5:
    			{
    				storephone();
    				break;
    			}
    			case 6:
    			{
    				restorephone();
    				break;
    			}
    			case 9:
    			{
    				menu();
    				break;
    			}
    			
    			default:
    			{
    				menu();
    				break;
    			}
    		}
    	}
    }
    compiler errors:
    /home/???/simple2/src/simple2.cpp: In function ‘void storename()’:
    /home/???/simple2/src/simple2.cpp:75: error: reference to ‘string’ is ambiguous
    /home/???/simple2/src/simple2.cpp:8: error: candidates are: char string [50]
    /usr/lib/gcc/i586-mandriva-linux-gnu/4.1.1/../../../../include/c++/4.1.1/bits/stringfwd.h:60: error: typedef struct std::basic_string<char, std::char_traits<char>, std::allocator<char> > std::string
    /home/???/simple2/src/simple2.cpp:75: error: reference to ‘string’ is ambiguous
    /home/???/simple2/src/simple2.cpp:8: error: candidates are: char string [50]
    /usr/lib/gcc/i586-mandriva-linux-gnu/4.1.1/../../../../include/c++/4.1.1/bits/stringfwd.h:60: error: typedef struct std::basic_string<char, std::char_traits<char>, std::allocator<char> > std::string
    gmake: *** [simple2.o] Error 1
    gmake: Target `simple2' not remade because of errors.
    *** Exited with status: 2 ***

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, if you insist on
    Code:
    using namespace std;
    Then don't use a name that is already in use in the std namespace:
    Code:
    char string[50];
    std::string is already defined, and you've just told the compiler "I want anything in std namespace to be usable from here on". [And no, the right solution isn't to move using namespace std a few lines further down].

    Just rename the variable to something that isn't used in std namespace.

    Or remove your using namespace std, and use this
    Code:
       using std::cout;
       using std::cin;
    ...
    as that (and a few more bits) are probably what you need from std namespace.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  11. #11
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    You should probably be using C++ strings anyway. To do that add a #include <string> and change char string[50] to string name; and change cin.getline ( string, 50, "/n"); to getline(cin, name);

  12. #12
    Registered User
    Join Date
    Sep 2007
    Posts
    29
    thank you all for helping me but i still have one ???. how do i catsh false inputs? EX. if the user types gt were my app asks for the users age how do i have my app reject the input?

  13. #13
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    Here's a generic way to read in a number that re-prompts when the user enters a letter. It clears the fail bit that is set because of the bad input, then ignores the bad input so the user can try again:
    Code:
    while (!(cin >> number))
    {
      cin.clear();
      cin.ignore(numeric_limits<streamsize>::max(), '\n');
      // cout an error message here and prompt again.
    }
    You'll want to #include <limits> and <ios> for the stuff in the ignore() line, or just use something like cin.ignore(1000, '\n') which will work as long as the user doesn't type in more than 1000 characters.

    You'll have to adapt that to your code.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21