Results 1 to 13 of 13

Thread: C++ Help

  1. #1
    Senior Member crazyfool's Avatar
    Join Date
    Jan 2008
    Posts
    761
    Thanks
    77
    Thanked
    38 times in 38 posts
    • crazyfool's system
      • Motherboard:
      • Striker Extreme
      • CPU:
      • Q6600
      • Memory:
      • OCZ 6400 2 x 2GB
      • Storage:
      • Samsung Spinpoint 500GB
      • Graphics card(s):
      • BFG 8800 GT OC 512MB
      • PSU:
      • Coolermaster Real Power Pro 850w
      • Case:
      • Coolermaster CM-690
      • Operating System:
      • XP, Vista, Windows 7 & Ubuntu
      • Monitor(s):
      • Samsung 2232BW

    C++ Help

    hey guys,
    I have just started my latest piece of coursework but i am having problems with a couple of things. Firstly i have used this method:
    string something
    ifstream file ("sometext.txt");
    file >> something
    This reads a text file into the string and then i have some code which seperates each word into a vector. This all works fine but... I need to be able to know when a then end of a line within the text being stored in a vector is reached. I know if i had used the getline method this would have been possible but before i go rewriting my code i wanted to know if there was another way.
    Secondly each vector stores a word and another vector stores numbers... how can i sort the vectors alphabetically and the other in numerically... any advice would be appreciated

  2. #2
    Gentoo Ricer
    Join Date
    Jan 2005
    Location
    Galway
    Posts
    11,048
    Thanks
    1,016
    Thanked
    944 times in 704 posts
    • aidanjt's system
      • Motherboard:
      • Asus Strix Z370-G
      • CPU:
      • Intel i7-8700K
      • Memory:
      • 2x8GB Corsiar LPX 3000C15
      • Storage:
      • 500GB Samsung 960 EVO
      • Graphics card(s):
      • EVGA GTX 970 SC ACX 2.0
      • PSU:
      • EVGA G3 750W
      • Case:
      • Fractal Design Define C Mini
      • Operating System:
      • Windows 10 Pro
      • Monitor(s):
      • Asus MG279Q
      • Internet:
      • 240mbps Virgin Cable

    Re: C++ Help

    Use a heap of strings, what's the purpose of using vectors for each word? Classic case of over-engineering trivial problems.
    Quote Originally Posted by Agent View Post
    ...every time Creative bring out a new card range their advertising makes it sound like they have discovered a way to insert a thousand Chuck Norris super dwarfs in your ears...

  3. #3
    Senior Member crazyfool's Avatar
    Join Date
    Jan 2008
    Posts
    761
    Thanks
    77
    Thanked
    38 times in 38 posts
    • crazyfool's system
      • Motherboard:
      • Striker Extreme
      • CPU:
      • Q6600
      • Memory:
      • OCZ 6400 2 x 2GB
      • Storage:
      • Samsung Spinpoint 500GB
      • Graphics card(s):
      • BFG 8800 GT OC 512MB
      • PSU:
      • Coolermaster Real Power Pro 850w
      • Case:
      • Coolermaster CM-690
      • Operating System:
      • XP, Vista, Windows 7 & Ubuntu
      • Monitor(s):
      • Samsung 2232BW

    Re: C++ Help

    lol nah i have to use vectors because i dont know the amount of words i'll need to store... plus it says i have to on the coursework lol

  4. #4
    Gentoo Ricer
    Join Date
    Jan 2005
    Location
    Galway
    Posts
    11,048
    Thanks
    1,016
    Thanked
    944 times in 704 posts
    • aidanjt's system
      • Motherboard:
      • Asus Strix Z370-G
      • CPU:
      • Intel i7-8700K
      • Memory:
      • 2x8GB Corsiar LPX 3000C15
      • Storage:
      • 500GB Samsung 960 EVO
      • Graphics card(s):
      • EVGA GTX 970 SC ACX 2.0
      • PSU:
      • EVGA G3 750W
      • Case:
      • Fractal Design Define C Mini
      • Operating System:
      • Windows 10 Pro
      • Monitor(s):
      • Asus MG279Q
      • Internet:
      • 240mbps Virgin Cable

    Re: C++ Help

    I see, that figures. Does the coursework specify atomising the strings into words?.. Or just using vectors for 'storage'?..
    I'd avoid that mess as much as possible unless I were to structure it. Otherwise the vector items become meaningless chunks of bytes. And yes, getline() would be easier, but you can just as easily look for EOL markers yourself, they're no different than spaces or any other character as far as string is concerned. As long as you remember to keep them in your vector entries you shouldn't have much of a problem.
    Quote Originally Posted by Agent View Post
    ...every time Creative bring out a new card range their advertising makes it sound like they have discovered a way to insert a thousand Chuck Norris super dwarfs in your ears...

  5. #5
    Senior Member crazyfool's Avatar
    Join Date
    Jan 2008
    Posts
    761
    Thanks
    77
    Thanked
    38 times in 38 posts
    • crazyfool's system
      • Motherboard:
      • Striker Extreme
      • CPU:
      • Q6600
      • Memory:
      • OCZ 6400 2 x 2GB
      • Storage:
      • Samsung Spinpoint 500GB
      • Graphics card(s):
      • BFG 8800 GT OC 512MB
      • PSU:
      • Coolermaster Real Power Pro 850w
      • Case:
      • Coolermaster CM-690
      • Operating System:
      • XP, Vista, Windows 7 & Ubuntu
      • Monitor(s):
      • Samsung 2232BW

    Re: C++ Help

    ok i fixed that problem... now i have a new one :<... if i have a string x how can i look through the string to see if it contains a certain word or number

  6. #6
    Gentoo Ricer
    Join Date
    Jan 2005
    Location
    Galway
    Posts
    11,048
    Thanks
    1,016
    Thanked
    944 times in 704 posts
    • aidanjt's system
      • Motherboard:
      • Asus Strix Z370-G
      • CPU:
      • Intel i7-8700K
      • Memory:
      • 2x8GB Corsiar LPX 3000C15
      • Storage:
      • 500GB Samsung 960 EVO
      • Graphics card(s):
      • EVGA GTX 970 SC ACX 2.0
      • PSU:
      • EVGA G3 750W
      • Case:
      • Fractal Design Define C Mini
      • Operating System:
      • Windows 10 Pro
      • Monitor(s):
      • Asus MG279Q
      • Internet:
      • 240mbps Virgin Cable

    Re: C++ Help

    Would string::find() do?
    Quote Originally Posted by Agent View Post
    ...every time Creative bring out a new card range their advertising makes it sound like they have discovered a way to insert a thousand Chuck Norris super dwarfs in your ears...

  7. #7
    Get in the van. Fraz's Avatar
    Join Date
    Aug 2007
    Location
    Bristol
    Posts
    2,919
    Thanks
    284
    Thanked
    397 times in 231 posts
    • Fraz's system
      • Motherboard:
      • Gigabyte X58A-UD5
      • CPU:
      • Watercooled i7-980X @ 4.2 GHz
      • Memory:
      • 24GB Crucial DDR3-1333
      • Storage:
      • 240 GB Vertex2E + 2 TB of Disk
      • Graphics card(s):
      • Water-cooled Sapphire 7970 @ 1175/1625
      • PSU:
      • Enermax Modu87+
      • Case:
      • Corsair 700D
      • Operating System:
      • Linux Mint 12 / Windows 7
      • Monitor(s):
      • Dell 30" 3008WFP and two Dell 24" 2412M
      • Internet:
      • Virgin Media 60 Mbps

    Re: C++ Help

    Quote Originally Posted by crazyfool View Post
    Secondly each vector stores a word and another vector stores numbers... how can i sort the vectors alphabetically and the other in numerically... any advice would be appreciated
    The STL provides several algorithms to sort elements of a range. Make sure you're not using the incorrect data structures in the first place though and are sorting to correct this problem!

    for instance there is:

    void std::sort(RandomAccessIterator begin, RandomAccessIterator end).

    This will sort by default according to the "<" operator, which should do what you want in both cases.

    You get the "begin" and "end" iterators from your vectors like this:

    myVector.begin()
    myVector.end()


    Do they actually teach you any C++ before they send you off with this coursework?!

    I suggest you buy this book:

    http://www.amazon.co.uk/C-Standard-L.../dp/0201379260

  8. #8
    Get in the van. Fraz's Avatar
    Join Date
    Aug 2007
    Location
    Bristol
    Posts
    2,919
    Thanks
    284
    Thanked
    397 times in 231 posts
    • Fraz's system
      • Motherboard:
      • Gigabyte X58A-UD5
      • CPU:
      • Watercooled i7-980X @ 4.2 GHz
      • Memory:
      • 24GB Crucial DDR3-1333
      • Storage:
      • 240 GB Vertex2E + 2 TB of Disk
      • Graphics card(s):
      • Water-cooled Sapphire 7970 @ 1175/1625
      • PSU:
      • Enermax Modu87+
      • Case:
      • Corsair 700D
      • Operating System:
      • Linux Mint 12 / Windows 7
      • Monitor(s):
      • Dell 30" 3008WFP and two Dell 24" 2412M
      • Internet:
      • Virgin Media 60 Mbps

    Re: C++ Help

    Here is some example code.

    Code:
    #include <vector>
    #include <string>
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
      vector<string> myStringVec;
      myStringVec.push_back("ba");
      myStringVec.push_back("ab");
      sort(myStringVec.begin(), myStringVec.end());
      cout << myStringVec.at(0) << myStringVec.at(1) << endl;  // prints "abba" rather than "baab".
      return 0;
    }

  9. #9
    Senior Member crazyfool's Avatar
    Join Date
    Jan 2008
    Posts
    761
    Thanks
    77
    Thanked
    38 times in 38 posts
    • crazyfool's system
      • Motherboard:
      • Striker Extreme
      • CPU:
      • Q6600
      • Memory:
      • OCZ 6400 2 x 2GB
      • Storage:
      • Samsung Spinpoint 500GB
      • Graphics card(s):
      • BFG 8800 GT OC 512MB
      • PSU:
      • Coolermaster Real Power Pro 850w
      • Case:
      • Coolermaster CM-690
      • Operating System:
      • XP, Vista, Windows 7 & Ubuntu
      • Monitor(s):
      • Samsung 2232BW

    Re: C++ Help

    EDIT: Ok can someone tell me wot is wrong with this...
    string xx = "1, 2, 3, "
    string x = "4"
    if xx.find(x) = false then
    do something
    else
    do something else
    it doesn't seem to find a match in the string if there is one e.g. x = 1 and if there isn't it doesn't seem to return false...HELP!!!!!!
    Last edited by crazyfool; 19-11-2008 at 08:20 PM.

  10. #10
    Get in the van. Fraz's Avatar
    Join Date
    Aug 2007
    Location
    Bristol
    Posts
    2,919
    Thanks
    284
    Thanked
    397 times in 231 posts
    • Fraz's system
      • Motherboard:
      • Gigabyte X58A-UD5
      • CPU:
      • Watercooled i7-980X @ 4.2 GHz
      • Memory:
      • 24GB Crucial DDR3-1333
      • Storage:
      • 240 GB Vertex2E + 2 TB of Disk
      • Graphics card(s):
      • Water-cooled Sapphire 7970 @ 1175/1625
      • PSU:
      • Enermax Modu87+
      • Case:
      • Corsair 700D
      • Operating System:
      • Linux Mint 12 / Windows 7
      • Monitor(s):
      • Dell 30" 3008WFP and two Dell 24" 2412M
      • Internet:
      • Virgin Media 60 Mbps

    Re: C++ Help

    Quote Originally Posted by crazyfool View Post
    EDIT: Ok can someone tell me wot is wrong with this...
    string xx = "1, 2, 3, "
    string x = "4"
    if xx.find(x) = false then
    do something
    else
    do something else
    it doesn't seem to find a match in the string if there is one e.g. x = 1 and if there isn't it doesn't seem to return false...HELP!!!!!!
    You have at least two problems here:

    Firstly you are using a single = sign when trying to do an "equals to" comparison (I doubt this would compile anyway, so maybe you aren't actually making this mistake).

    Secondly, the find function returns string::npos if it fails to find the substring you are searching for (this is probably (2^32)-1 or some such number). If it does find the substring you are looking for then it returns the index of the first character of the substring in the string in which you are performing the search.

    So... if you set x = 1, this is at position 0 of xx. Thus your logic will claim it hasn't found this string even when it has (the find method will be returning zero which also equates to false). In any other circumstance your code will be claiming it has found the string regardless of what actually has happened.
    Last edited by Fraz; 20-11-2008 at 02:17 PM.

  11. Received thanks from:

    crazyfool (20-11-2008)

  12. #11
    Senior Member crazyfool's Avatar
    Join Date
    Jan 2008
    Posts
    761
    Thanks
    77
    Thanked
    38 times in 38 posts
    • crazyfool's system
      • Motherboard:
      • Striker Extreme
      • CPU:
      • Q6600
      • Memory:
      • OCZ 6400 2 x 2GB
      • Storage:
      • Samsung Spinpoint 500GB
      • Graphics card(s):
      • BFG 8800 GT OC 512MB
      • PSU:
      • Coolermaster Real Power Pro 850w
      • Case:
      • Coolermaster CM-690
      • Operating System:
      • XP, Vista, Windows 7 & Ubuntu
      • Monitor(s):
      • Samsung 2232BW

    Re: C++ Help

    cheerz m8 got that sorted now... now i have another seperate problem
    int i = 0;
    i = i + 1;
    char c = i;
    string s = c;
    this code is used to print out a series of numbers however after it gets to number eight it starts print symbols and letters... i only used this code to convert int to a string but i cant use itoa because it is not allowed... anyone know how to fix this

  13. #12
    Get in the van. Fraz's Avatar
    Join Date
    Aug 2007
    Location
    Bristol
    Posts
    2,919
    Thanks
    284
    Thanked
    397 times in 231 posts
    • Fraz's system
      • Motherboard:
      • Gigabyte X58A-UD5
      • CPU:
      • Watercooled i7-980X @ 4.2 GHz
      • Memory:
      • 24GB Crucial DDR3-1333
      • Storage:
      • 240 GB Vertex2E + 2 TB of Disk
      • Graphics card(s):
      • Water-cooled Sapphire 7970 @ 1175/1625
      • PSU:
      • Enermax Modu87+
      • Case:
      • Corsair 700D
      • Operating System:
      • Linux Mint 12 / Windows 7
      • Monitor(s):
      • Dell 30" 3008WFP and two Dell 24" 2412M
      • Internet:
      • Virgin Media 60 Mbps

    Re: C++ Help

    Quote Originally Posted by crazyfool View Post
    cheerz m8 got that sorted now... now i have another seperate problem

    this code is used to print out a series of numbers however after it gets to number eight it starts print symbols and letters... i only used this code to convert int to a string but i cant use itoa because it is not allowed... anyone know how to fix this
    If you are using a stream insertion operator (i.e. <<), it should take care of this for you anyway:

    Code:
    int myInt = 58; 
    cout << myInt << endl;
    will print 58 to the screen.

    If you need to convert an integer for some other purpose, you can use ostringstreams:

    Code:
      int myInt = 58;
      ostringstream oss;
      oss << myInt;
      string myIntAsString = oss.str();
    Not the most CPU-efficient way possible, mind.

    By the way, what you were doing is the most god-awful hack imaginable

  14. Received thanks from:

    crazyfool (20-11-2008)

  15. #13
    Senior Member crazyfool's Avatar
    Join Date
    Jan 2008
    Posts
    761
    Thanks
    77
    Thanked
    38 times in 38 posts
    • crazyfool's system
      • Motherboard:
      • Striker Extreme
      • CPU:
      • Q6600
      • Memory:
      • OCZ 6400 2 x 2GB
      • Storage:
      • Samsung Spinpoint 500GB
      • Graphics card(s):
      • BFG 8800 GT OC 512MB
      • PSU:
      • Coolermaster Real Power Pro 850w
      • Case:
      • Coolermaster CM-690
      • Operating System:
      • XP, Vista, Windows 7 & Ubuntu
      • Monitor(s):
      • Samsung 2232BW

    Re: C++ Help

    dude you are awesome lol... last question i promise... is there a way for me to remove certain characters from string... e.g.
    string str = "hexus-forums"
    how can i remove the "-" to leave it as "hexusforums"

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •