About vectors.

This is a discussion on About vectors. within the C++ Programming forums, part of the General Programming Boards category; Suppose I want the user himself to enter the data into a vector. How should I go about doing it? ...

  1. #1
    Registered User
    Join Date
    Aug 2008
    Posts
    30

    About vectors.

    Suppose I want the user himself to enter the data into a vector. How should I go about doing it? cin wouldn't work, neither does cin.get.

    Example:
    Code:
    #include <iostream>
    #include <vector>
    
    using namespace std;
    
    void main (void)
    {
        vector<char>alphabet;
        //user inputs data...but how? fill in please =)
        for(int i = 0; i < alphabet.size(); i++)
        {
            cout << alphabet[i];
        }
        cout << endl;
        system("pause");
    }
    Also, there was a warning on using alphabet.size() over there while the program is as incomplete as it is.
    Warning C4018: '<' : signed/unsigned mismatch

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You should probably use push_back to add things to a vector.

    And in this case your signed/unsigned warning is harmless, but the easy way to fix it is to declare i as unsigned.

  3. #3
    Registered User
    Join Date
    Aug 2008
    Posts
    30
    What if I want the user to input data in like a character array, where the user can just type in a string of characters immediately and it goes into the character array? I could use a string vector but I need to be able to edit each character so I probably can't use a string vector.

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    If a vector<char> is good enough, why don't you just use a string to store the input?

    Also, what makes you think that you cannot edit each character in a string vector?
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #5
    Registered User
    Join Date
    Aug 2008
    Posts
    30
    Well umm...I'm trying to learn to use vectors so I have to use a vector xD and how do I change the individual characters in a string? Maybe you could somehow add to my code here.
    Code:
    #include <iostream>
    #include <algorithm>
    #include <vector>
    
    using namespace std;
    
    void main (void)
    {
    vector<char>alphabet;
    while(1)
    {
    	if(alphabet.size() > 10)
    	{
    		cin.clear();
    		cin.ignore(100,'\n');
    		cout << "Too many letters! Max 10 letters are allowed : ";
    	}
    	else
    	{
    		break;
    	}
    }
    for(unsigned int a = 0; a < alphabet.size(); a++)
    {
    	cout << alphabet[a];
    }
    cout << ", ";
    for (unsigned int b = 0; b < alphabet.size(); b++)
    {
    	if(static_cast<int>(alphabet[b]>= 65 && static_cast<int>(alphabet[b] <= 90)))
    	{
    		alphabet[b] = static_cast<char>(static_cast<int>(alphabet[b]) + 32);
    	}
    	else if(static_cast<int>(alphabet[b]>= 91 && static_cast<int>(alphabet[b] <= 122)))
    	{
    		alphabet[b] = static_cast<char>(static_cast<int>(alphabet[b]) - 32);
    	}
    	cout << alphabet[b];
    }
    cout << endl;
    system("pause");
    }

  6. #6
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    void main (void)
    int main (void) - read FAQ

    Code:
    if(alphabet.size() > 10)
    and where is the code that make this test possibly true?

    Code:
    cout << alphabet[a];
    Do you plan to initialize alphabet with something before outputting?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  7. #7
    The larch
    Join Date
    May 2006
    Posts
    3,573
    If you manage to store anything in your vector, then all the static_casts are unnecessary too, as they would happen implicitly anyway.

    You access individual characters in a string just as you do with the vector, using the [] operator.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Your use of magic numbers (65, 90, 91, 122) is a bad thing. Use the appropriate character constants instead. (This would also make it more explicit that you are trying to uppercase things like '['.)

    You're bringing in the big guns with <algorithm> but I don't see why.

    As mentioned before, you can add a char at a time to a vector<char> so you'll have to read things in a char at a time. But, checking whether alphabet.size() > 10 doesn't actually remove the extra bits from alphabet. You should check during input that only 10 letters are input.

  9. #9
    Registered User
    Join Date
    Aug 2008
    Posts
    30
    I guess the code was pretty messy huh. This is the reworked code and it works fine.
    Code:
    #include <iostream>
    #include <string>
    #include <vector>
    
    using namespace std;
    
    void main (void)
    {
    	char word[100];
    	vector<char>alphabet;
    	while(1)
    	{
    		cin >> word;
    		for(unsigned int i = 0; word[i] != '\0'; i++)
    		{
    			alphabet.push_back(word[i]);
    		}
    		if(alphabet.size() > 10)
    		{
    			cin.clear();
    			cin.ignore(100,'\n');
    			alphabet.clear();
    			cout << "Too many letters! Max 10 letters are allowed : ";
    		}
    		else
    		{
    			break;
    		}
    	}
    	for(unsigned int a = 0; a < alphabet.size(); a++)
    	{
    			cout << alphabet[a];
    	}
    	cout << ", ";
    	for (unsigned int b = 0; b < alphabet.size(); b++)
    	{
    		if(static_cast<int>(alphabet[b]>= 65 && static_cast<int>(alphabet[b] <= 90)))
    		{
    			alphabet[b] = static_cast<char>(static_cast<int>(alphabet[b]) + 32);
    		}
    		else if(static_cast<int>(alphabet[b]>= 91 && static_cast<int>(alphabet[b] <= 122)))
    		{
    			alphabet[b] = static_cast<char>(static_cast<int>(alphabet[b]) - 32);
    		}
    		cout << alphabet[b];
    	}
    	cout << endl;
    	system("pause");
    }
    However, could you further explain how else I can check for upper and lower case letters?

    As for not using int main() well, my course is currently teaching me to use void main() so I suppose its better to do so for now.

    I also do not understand why exactly would I not need to use static_cast<int>. Does it automatically check for the int value of the character if I'm checking with an int value?

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    There are such things as isupper() and islower(). If you feel you must use ASCII code comparisons, you should use a literal 'a' and 'z' and a literal 'A' and 'Z' in place of 65, etc. (Just for fun, type in "bob]george" and still see if you claim it's working.)

    Your belief that characters are not integers is charming, but incorrect. If you remove all the static_casts in your program (both to int and to char) it will work in exactly the same way. (I suppose there's nothing wrong with being explicit as to when you mean to interpret things as characters and when you mean to interpret things as integers, but it's irrelevant to C++.)

  11. #11
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Why to work so hard for filling vector of chars? Just 2 days ago was a sample for this simple task

    Code:
    string s;
    vector<char> v;
    if(cin >> s)
        v.insert(v.end(), s.begin(), s.end());
    About vectors.

    and still you have main as void
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  12. #12
    Registered User
    Join Date
    Aug 2008
    Posts
    30
    Quote Originally Posted by vart View Post
    and still you have main as void
    Takes time to change and I'm trying to. xD

    Ok thanks for the info on isupper() and islower() it is REALLY very helpful.

    By the way, though a little off-topic, I must ask how should I continue to learn C++? I have learned that just waiting to be taught in lesson is not enough, but the thing is I don't know how to go about doing it. Just like people usually start with cin and not GetAsyncKeyState, is there possibly a way to learn C++ in order? I'm currently at Classes and have jumped to <vector>. I'm also trying to understand <map>, though this pace feels kinda wrong. =(

  13. #13
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I'd recommend that you buy the book "Accelerated C++". It is one of the better of the MANY books written to teach C++. Follow the order of that book.

    Of course, you don't need to know everything about everything in C++ to be able to write useful, meaningful and efficient programs - because some types of programs only use some parts of the C++ language and related libraries.

    On the other hand of "you only need to know some parts" is "if you don't know it exists, you can't use it when you need it". So having a brief understanding that there is something called "map", and it works sort of like an array where the index can be "anything" is useful to know even if you have no need at all for maps in the code you are writing/planning to write at present.

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

  14. #14
    Registered User
    Join Date
    Aug 2008
    Posts
    30
    How much does that book cover actually? and are there some good websites with at least a good list that tells me what I should learn in order? xD

  15. #15
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I haven't actually read that book - it is what most people recommend however [there is a book topic in the C++ forum].

    Once you have the basics, such as simple variables, input/output, flow-control, basic functions covered, and basic user defined data types (struct, enum, union), it is pretty much arbitrary how you continue from there, because you have the fundamentals. Of course there are still some things that depend on understanding lower level stuff, e.g. templates to make classes requires that you know classes.

    I'm sure there are a whole heap of different web-sites with varying levels of "this is the order to learn things".

    Another approach to learning is to set yourself a project [that you have reasonable confidence that you can solve], and start programming that project. A calculator is always a good example, because you can start simple, expand and extend it until you have a graphical user interface and a small programming language that allows you to program user specified functions.

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

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Vectors
    By naseerhaider in forum C++ Programming
    Replies: 11
    Last Post: 05-09-2008, 08:21 AM
  2. How can i made vectors measuring program in DevC++
    By flame82 in forum C Programming
    Replies: 1
    Last Post: 05-07-2008, 02:05 PM
  3. How properly get data out of vectors of templates?
    By 6tr6tr in forum C++ Programming
    Replies: 4
    Last Post: 04-15-2008, 10:35 AM
  4. How to use Vector's in C++ !?!
    By IndioDoido in forum C++ Programming
    Replies: 3
    Last Post: 10-14-2007, 11:13 AM
  5. Points, vectors, matrices
    By subnet_rx in forum Game Programming
    Replies: 17
    Last Post: 01-11-2002, 01:29 PM

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