i cant get this program to work

This is a discussion on i cant get this program to work within the C++ Programming forums, part of the General Programming Boards category; Code: // program that uses switch statement to allow the user the choose what memeber //function to be tested #include<iostream> ...

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    6

    i cant get this program to work

    Code:
    // program that uses switch statement to allow the user the choose what memeber //function to be tested
    
    
    #include<iostream>
    using namespace std;
    #include<vector>
    using std::vector;
    
    #include <iomanip>
    using std::setw;
    
    void pop_back();//removes the last element from vector
    void push_back(const T& x);//stores a value in the last element of the vector
    void resize ( size_type sz, T c = T() );//change the size of vector
    void clear();//clears vectore from all elements
    void reverse();//reverse the oreder of the element in vector
    void outputVector( const vector< int > & );
    void inputVector( vector< int > & );
    vector < int> integers (5); //vector declaration
    int  main()
    {
    int function;// switch statement variable
    
    
    cout <<"Size of vector integers is "<< integers.size()
            <<"\nvector after initialization" << endl;
         outputVector( integers );
     cout << "\nEnter 5 integers:"<< endl;
         inputVector( integers );
    
    cout <<"\nAfter input, the vectors contain:\n"
            <<"integers" << endl;
         outputVector( integers );
    cout<<" Enter a number to choose function\n";
       <<"1 to pop_back\n";
       <<"2 to push_back\n";
       <<"3 to resize\n;
       <<"4 to clear vector\n"
        <<"5 to reverse vectore"<<endl;
        cin>>function;
    while (function != EOF)
    {
    switch (function)
    {
    case 1:
    cout<<" the new vector is"<<integers.pop_back()<<endl;
    break;
    case 2:
    cout<<" the new vectore is"<<integers.push_back()<<endl;
    break;
    case 3:
    cout<<" the new vector is"<<integers.resize(7,11)<<endl;
    break;
    case 4:
    
    cout<<the  new vector is"<<integers.clear()<<endl;
    
    break;
    case 5:
    cout<<"the new vector is "<<integers.reverse()<<endl;
    break;
     default:
    cout<<"incorrect function number"
    <<"enter a new function number"<<endl;
    break;
    }//end switch
    }//end while
    return 0;
    }// end main
    
    void pop_back()
    {
    for( size_t i = 0; i < integers.size(); i++)
    {
    cout<<setw(12)<<integers[i];
    }
    }
    void push_back(const T& x)
    {
     for (size_t i =0; i < integers.size(); i++)
    {
    cout<<setw(12)<<integers.push_back(7);
    }
    }
    void clear()
    {
    for ( size_t i= 0; i < integers.size(); i++)
    {
    cout<<setw(12)<<integers.clear();
    }
    }
    void  resize( size_type sz, T c = T() )
    {
    for ( size_t i =0; i < integers.size(); i++)
    {
    cout<<setw(12)<<integers.resize(7,11);
    }
    }
    void reverse()
    {
    for(size_t i = 0; i<integers.size(); i++)
    {
    cout<<setw(12)<<integers.reverse();
    }
    }
    
    void outputVector( const vector< int > &array )
      {
         size_t i; // declare control variable
    
         for ( i = 0; i < array.size() ; i++ )
         {
            cout << setw( 12 ) << array[ i ] ;
    
            if (( i + 1 ) % 4 == 0 ) // 4 numbers per row of output
               cout << endl;
         } // end for
    
        if ( i % 4 != 0 )
           cout << endl;
    } // end function outputVector
    
     // input vector contents
    void inputVector( vector< int > &array )
     {
        for (  size_t i = 0; i < array.size(); i++ )
           cin >> array[ i ] ;
     } // end function inputVector

  2. #2
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,655
    Learn how to ask a question. "I can't get this program to work" is not a question, it's a statement. Tell us what errors you're receiving, or if you're not receiving errors, what you expect this program to do and what it is doing instead.

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    6
    here are the errors that i am getting


    vector.cpp:15: syntax error before `&' token
    vector.cpp:16: `size_type' was not declared in this scope
    vector.cpp:16: syntax error before `,' token
    vector.cpp: In function `int main()':
    vector.cpp:37: syntax error before `<<' token
    vector.cpp:39:6: warning: multi-line string literals are deprecated
    vector.cpp:39: stray '\' in program
    vector.cpp:40:26: warning: multi-line string literals are deprecated
    vector.cpp:41:28: warning: multi-line string literals are deprecated
    vector.cpp:48:26: warning: multi-line string literals are deprecated
    vector.cpp:51:27: warning: multi-line string literals are deprecated
    vector.cpp:54:26: warning: multi-line string literals are deprecated
    vector.cpp:61: case label `5' not within a switch statement

  4. #4
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    do you want to write template finctions of functions that work with integers?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  5. #5
    Registered User
    Join Date
    Jan 2009
    Posts
    6
    /usr/include/c++/3.2.3/iomanip:128: std::basic_ostream<_CharT,
    _Traits>& std:perator<<(std::basic_ostream<_CharT, _Traits>&,
    std::_Setbase) [with _CharT = char, _Traits = std::char_traits<char>]
    what does this error mean?
    what i was trying to do is use the vector member functions as prototype am not sure if template that i wrote was correct. Then call these fuctions in main within the switch statement. i did fix the syntax errors but i keep getting them everytime i compile in addition to the above error.
    i apologize for not being clear from the beginning, this is my first time am using this site.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I think you are trying to print (using cout << ) things that aren't explicitly printable. You may be able to define a way to print them, but most likely, you don't really want to do what you've asked the compiler to do...

    For example:
    Code:
    integers.resize(7,11)
    is a void function - so how do you expect that to produce some sensible output?

    Code:
    cout<<setw(12)<<integers.reverse();
    There is no vector::reverse() function. There is a reverse algorithm that takes a pair of "start" and "end" iterators, but vectors do not have a reverse function [no need for it].

    You seem to have function prototypes for a number of functions that are actually part of std::vector - you do not need to declare those [and certainly not in the way you have done it].

    --
    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.

  7. #7
    Registered User
    Join Date
    Jan 2009
    Posts
    6
    can i use vector memebr function as prototype? i am getting confused on how to use those functions in a switch statement

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I think you are confused about prototypes, first of all. Prototypes are declarations of functions, so that the compiler knows what arguments and result-type the function has.

    You can certainly use switch to select things to do on a vector, but printing a function that has a "void" result will never work - void (at least in this context) means "of no value" or "of no use", and you can't print something that is "of no value". So "cout << integers.resize(...)" will not work, ever! If you explain what you actually expect this to do, perhaps we can tell you how to achieve what you expect.

    And of course, you can not call member functions of a class, where there are no such member function - so integers.reverse() will also never compile. And no, you can't extend the vector class - it is not intended to be extended.

    --
    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
    Jan 2009
    Posts
    6
    what i am expecting this program to do is to ask the user to input the vector then choose what function to be performed on this vector. so i was thinking to use switch statementto perform this task. Do i have to declare the functions outside main or not necessary? If you could give me an example of one function inside switch will be great help.
    Thank you

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    First of all, no, you do not need to declare functions at all in this case, except for the functions you yourself is defining (the input and output vector functions), since they are part of the vector class, which you have dragged in the declaration of by using the #include <vector> line.

    I don't see that calling functions in switch as the problem - the problem is that you are feeding things to cout that can not be printed. That has nothing to do with being inside a switch-statement. If you have problems with switch as such, then you probably should have a look at the tutorials section or chapter in the book that talks about switch statements.

    --
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Client-server system with input from separate program
    By robot-ic in forum Networking/Device Communication
    Replies: 3
    Last Post: 01-16-2009, 03:30 PM
  2. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 11:17 PM
  3. threaded program doesn't work?
    By OcTO_VIII in forum Linux Programming
    Replies: 1
    Last Post: 12-11-2003, 12:37 PM
  4. The Bludstayne Open Works License
    By frenchfry164 in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 11-26-2003, 11:05 AM
  5. how does this program work
    By ssjnamek in forum C++ Programming
    Replies: 2
    Last Post: 01-02-2002, 01:48 PM

Tags for this Thread


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