Thread: runtime cin error

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

    runtime cin error

    hi guys, here i have a pretty noobish question...

    say,

    Code:
    #include <iostream>
    using namespace std;
    
    class abc {
    
    int value;
    abc(char something) {
        switch(something) {
            case '1':
                cin >> value;
                break;
        }
    };
    
    main() {
    abc(1);
    return 0;
    }
    i have something like that, and theres no compiletime error, but when i run it, there is just some random error.

    and i am just wondering if someone know any good free c++ debuggers. thanks.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What C++ compiler are you using? That code should not even compile.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    32
    well it is not really the actual thing. Anyway, im using digital mars.

    Code:
    #include <iostream>
    #include <string>
    #include <vector>
    #include "tool.h"
    using namespace std;
    
    
    Key::Key(char choice) {
            cout << "Enter the key.";
    	getinfo(choice);
    }
    
    void Key::getinfo(char choice) {
    	switch(choice) {
    		case '1':
    		        cin >> value;cout << value;
    		break;
    	}
    }
    
    Cipher::Cipher() {
    	cout << "Enter the encrypted-text.";
    	cin >> Text;
    
    	length = Text.length();
    }
    
    
    int main() {
    	Menu();
    
    	return 0;
    }
    
    void Menu(void) {
        
        char choice;                                 // What to do
            
    // starting the menu              
        cout << "Menu:" << endl;
        cout << "   1. Sort the cipher text into shrift siphers according to the key length" << endl;
        cout << "   2. Frequency analysis the cipher-text." << endl;
        cout << "   3. Decrypt a monoalpheric cipher with a key." << endl;
        cout << "Enter q to quit.\n" << endl;
    
        cout << "Enter your choice: ";
        cin >> choice;
    
        switch(choice) {
            case '1' :
                SplitCode(choice);
                Menu();
                break;
            case '2' :
                Menu();
                break;
            case '3' :
                Menu();
                break;
            case 'q' :
                break;
            default :
                cout << "Please enter a valid choice." << endl;
                Menu();
                break;
        }
    
    }
    
    void SplitCode(char choice) {
        Cipher Vigenerec;                      // Create an instance of the cipher object
    
        Key Vigenerek(choice);                            // Create an instance of the key
    
        int Rmd = Vigenerek.value &#37; Vigenerec.length;// The number of odd digits out (uncompleted bits of key)
        int ArrayLen = (Vigenerec.length - Rmd) / Vigenerek.value;
        if(Rmd) ArrayLen++;
    
        vector<char> Split_Text[20];
    
        for(i=0;i<Vigenerek.value;++i) Split_Text[i].resize(ArrayLen);
        
        for(i=0;i<Vigenerec.length;++i) {
            int r = i % 5;                          // The bit of the key
            int r2 = (i-r)/5;                       // How many times did the key looped
    
            Split_Text[r][r2] = Vigenerec.Text[i];
        }
    
        for(i=0;i<Vigenerek.value;++i) {
            for(i1=0;i1<ArrayLen;i++) cout << Split_Text[i][i1];
    
            cout << "" << endl;
        }
    }
    
    
    
    
    #include <iostream>
    #include <string>
    #include <vector>
    using namespace std;
    
    // ///////////////variable declare
    int i, i1;                                      // Loop variables
    
    // ///////////////library functions declaration
    char * strcat ( char*, char const* );
    
    // ///////////////class or structures
    class Key {
        
    	public:
    
    	int value;                                 // The actual key for shrift keys
    	string code;                      // The key for other monoalpheric ciphers
    	int length;                                 // The length of the key
    
    	char Shrift_Key[2][26];              // The entire key for a shrift cipher
    
    	Key(char choice);                         // Constructor
    	void getinfo(char choice);
    };
    
    class Cipher {
    
        public:
    
        int length;                     // Length of array
        string Text;
    
        Cipher();                        // Constructor      
    };
    
    // /////////////////functions protype
    void SplitCode(char choice);
    
    void Menu(void);
    it is actually that.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Let the guess: the second part is tool.h?

    At a glance, there are a few potential problems:

    1. You have a using directive (using namespace std) in your header that will spillover to files that include this header. Instead, fully qualify names used in headers.

    2. Your header lacks header inclusion guards. This is not a problem here since you only include "tool.h" once, but could be a problem later.

    3. Declare variables near their first use. Your global variables i and i1 clearly violate that guideline.

    The problem you are currently facing may be from here:
    Code:
    for(i=0;i<Vigenerek.value;++i) {
        for(i1=0;i1<ArrayLen;i++) cout << Split_Text[i][i1];
    
        cout << "" << endl;
    }
    Notice that the outer loop uses i as the loop variable, the inner loop uses i1 as the loop variable, but the inner loop increment i instead of i1. Eventually, this would lead to an array out of bounds error for Split_Text.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Sep 2007
    Posts
    32
    thanks that really fixed the problem, but i still have a runtime error right after "cin >> value;" in void key::getinfo(). i put a cout there just to test it, but it wont exult either.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    i still have a runtime error right after "cin >> value;" in void key::getinfo(). i put a cout there just to test it, but it wont exult either.
    You could use a debugger to help you find out where exactly is the problem. If you somehow cannot use a debugger, then manually insert debug print statements in appropriate places, commenting out portions of code if necessary.

    At the moment, the classes that model your problem domain are tightly coupled to the user interface. I think that you will find that your code would be easier to test if SplitCode() did not do all the work, but instead Key and Cipher had appropriate (member) functions that did some of the work. These functions would merely handle the manipulation of the problem domain, with other functions doing the work of getting input from the user.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Testing some code, lots of errors...
    By Sparrowhawk in forum C Programming
    Replies: 48
    Last Post: 12-15-2008, 04:09 AM
  2. Connecting to a mysql server and querying problem
    By Diod in forum C++ Programming
    Replies: 8
    Last Post: 02-13-2006, 10:33 AM
  3. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Learning OpenGL
    By HQSneaker in forum C++ Programming
    Replies: 7
    Last Post: 08-06-2004, 08:57 AM