Thread: constant input

  1. #1
    Registered User
    Join Date
    Nov 2004
    Posts
    86

    constant input

    the code I'm using is this:

    //vector.h
    //does vector math

    #include <iostream>
    #include <string>
    #include <cmath>
    #include <cstdlib>

    using namespace std;
    using std::string;

    class vector {
    public:

    float *vector1;
    float *vector2;
    string vectorsWhole[2];
    string inread;
    bool last;
    int choice;

    bool lastElement(){

    if(inread.substr(inread.length()-2,inread.length()-1)=="l"){

    return true;

    }else{

    return false;

    }//end if

    }//end lastElement

    };//end class

    void vec(vector v){

    cout<<"Enter one of the following options:\n\nType 1 for addition/multiplication/subtraction/division\n\nType 2 for dot products\n";

    cin>>v.choice;

    switch(v.choice){

    case 1:

    cout<<"Enter the numbers in the first vector, 1 at a time\nadd the character l to the end of your last element:\n";

    while(v.lastElement()==false){

    cin>>v.inread;

    v.vectorsWhole[0]+=v.inread;

    if(v.lastElement()==false){

    v.vectorsWhole[0]+=",";

    }//end if

    }//end while

    cout<<v.vectorsWhole[0];

    break;

    default:

    break;

    //end case

    }//end switch

    }//end vec


    I know, the code is simple, don't laugh, especially at the switch statement

    anyway, As is seen in the while loop, I'm trying to get input until a character is appended to the last input

    whenever I compile it, I get no errors

    when I run it, as soon as it enters the while loop it exits "due to signal 6 (SIGABRT)"

    any insight?

  2. #2
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    Woop?

  3. #3
    Registered User
    Join Date
    Nov 2004
    Posts
    86
    I should have mentioned that "goodness no, it isn't nearly finished yet"

  4. #4
    Registered User
    Join Date
    Nov 2004
    Posts
    86
    since i'm getting hissed at for not using code tags...


    Code:
    //vector.h
    //does vector math
    
    #include <iostream>
    #include <string>
    #include <cmath>
    #include <cstdlib>
    
    using namespace std;
    using std::string;
    
    class vector {
    public:
    
    	float *vector1;
    	float *vector2;
    	string vectorsWhole[2];
    	string inread;
    	bool last;
    	int choice;
    	
    	bool lastElement(){
    	
    		if(inread.substr(inread.length()-2,inread.length()-1)=="l"){
    		
    			return true;
    			
    		}else{
    		
    			return false;
    			
    		}//end if
    		
    	}//end lastElement
    	
    };//end class
    
    void vec(vector v){
    
    	cout<<"Enter one of the following options:\n\nType 1 for addition/multiplication/subtraction/division\n\nType 2 for dot products\n";
    	
    	cin>>v.choice;
    	
    	switch(v.choice){
    	
    		case 1:
    		
    			cout<<"Enter the numbers in the first vector, 1 at a time\nadd the character l to the end of your last element:\n";
    			
    			while(v.lastElement()==false){
    			
    				cin>>v.inread;
    				
    				v.vectorsWhole[0]+=v.inread;
    				
    				if(v.lastElement()==false){
    				
    					v.vectorsWhole[0]+=",";
    					
    				}//end if
    				
    			}//end while
    			
    			cout<<v.vectorsWhole[0];
    			
    			break;
    			
    		default:
    		
    			break;
    			
    		//end case
    		
    	}//end switch
    	
    }//end vec

  5. #5
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Just a warning: There is already a template class called vector. You may want to rename your class as to avoid confusion.

  6. #6
    Registered User
    Join Date
    Nov 2004
    Posts
    86
    I renamed the class
    nothing changes

  7. #7
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Quote Originally Posted by pktcperlc++java
    I renamed the class
    nothing changes

    It wasn't meant to fix your problem, just to avoid confusion with an already existing STL container class by the name of vector. Your problem, whatever it is (haven't looked... too tired), still exists.
    "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

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    You call lastElement when entering the while loop. At that point, inread is empty and thus both inread.length()-1 and inread.length()-2 refer to invalid indices. You need to:
    1) Review the documentation of substr: the second parameter must be the length, not the end index.
    2) Check if the string is empty before doing the substring operation.
    3) Get the correct substring, which starts at length()-1, not -2.

    You also should:
    1) Use inread.back() to get the last character.
    2) Directly return the result of the comparison.
    3) Read a book about object-oriented design, to learn why and how your usage of classes here is wrong.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. input redirection
    By sashaKap in forum C Programming
    Replies: 6
    Last Post: 06-25-2009, 01:59 AM
  2. I would love some input on my BST tree.
    By StevenGarcia in forum C++ Programming
    Replies: 4
    Last Post: 01-15-2007, 01:22 AM
  3. About aes
    By gumit in forum C Programming
    Replies: 13
    Last Post: 10-24-2006, 03:42 PM
  4. Try out my new game :) !
    By Stan100 in forum Game Programming
    Replies: 10
    Last Post: 06-05-2003, 08:10 AM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM