New Posts  All Forums:Forum Nav:

Help with strings in C++

post #1 of 13
Thread Starter 
I'm still new to programming so don't laugh, but I'm having trouble with one problem my professor assigned.

It asks us to abbreviate state names, using the first 2 letters for single word states and the first letter of each word for 2 word states.

This is what I have so far, but I'm getting an error "string subscript out of range". Could anyone point out what I'm doing wrong?

There are probably easier ways to do this, but I don't want to use any information that we haven't learned yet.
Code:
#include <iostream>
#include <string>
#include <cctype>
using namespace std;

void ReadData(string& State);
string FindAbb(string State);
string DisplayAbb(string Abb);

int main()
{
        string State, Abb;
        char YesNo;

        do{
        ReadData(State);
        Abb = FindAbb(State);
        cout<<"\t"<<State<<" or "<<DisplayAbb(Abb);
        cout<<"\tContinue (y/n)? ";
        cin>>YesNo;
        }while (YesNo=='y' || YesNo=='Y');

        system("PAUSE");
        return 0;
}

void ReadData(string& State)
{
        cout<<"Enter a state name: ";
        getline(cin, State);
}

string FindAbb(string State)
{
        int pos=State.find(' ');
        if (pos==-1)
        {
                return State.substr(0,2);
        }
        else
        {

                return State.substr(0,1)+State.substr(pos+1,1);
        }

}

string DisplayAbb(string Abb)
{
        Abb[0] = toupper(Abb[0]);
        Abb[1] = toupper(Abb[2]);
        return Abb;
}
Power Edition
(18 items)
 
   
CPUMotherboardGraphicsRAM
i7 3610QM Clevo p151EMx Intel HM77 NVidia GTX 670m 16GB 1600Mhz DDR3 Samsung 4x4 
Hard DriveOptical DriveCoolingOS
Seagate Momentus 750GB 7200 RPM 16MB Cache TSSTcorp CDDVDW SN-208AB Sager NP9130 Internal Fans Windows 7 Ultimate 64 bit 
MonitorKeyboardPowerCase
15.6” 1920x1080 LED-Backlit Display Matte Finish Sager NP9130 Keyboard 8 Cell Smart Lithium-Ion Battery 76.96Wh Sager NP9130 
MouseAudio
Touchpad or Logitech G5 Onkyo Installed Speakers 
  hide details  
Reply
Power Edition
(18 items)
 
   
CPUMotherboardGraphicsRAM
i7 3610QM Clevo p151EMx Intel HM77 NVidia GTX 670m 16GB 1600Mhz DDR3 Samsung 4x4 
Hard DriveOptical DriveCoolingOS
Seagate Momentus 750GB 7200 RPM 16MB Cache TSSTcorp CDDVDW SN-208AB Sager NP9130 Internal Fans Windows 7 Ultimate 64 bit 
MonitorKeyboardPowerCase
15.6” 1920x1080 LED-Backlit Display Matte Finish Sager NP9130 Keyboard 8 Cell Smart Lithium-Ion Battery 76.96Wh Sager NP9130 
MouseAudio
Touchpad or Logitech G5 Onkyo Installed Speakers 
  hide details  
Reply
post #2 of 13
Take a look at your DisplayAbb function:
Code:
string DisplayAbb(string Abb)
{
        Abb[0] = toupper(Abb[0]);
        Abb[1] = toupper(Abb[2]);
        return Abb;
}


I didn't run your code, but with a quick glance it would seem you're trying to uppercase a letter in the Abb string that does not exist (since all state abbreviations are two letters, and array indexing in C++ starts at 0).
Morpheus Mini
(7 items)
 
  
CPUMotherboardGraphicsRAM
i7-6700T SN970 GTX 960 Crucial CT102464BF160B 
Hard DriveOSAudio
Samsung 850 Evo M.2 Windows 10 Pro MOTU Audio Express 
  hide details  
Reply
Morpheus Mini
(7 items)
 
  
CPUMotherboardGraphicsRAM
i7-6700T SN970 GTX 960 Crucial CT102464BF160B 
Hard DriveOSAudio
Samsung 850 Evo M.2 Windows 10 Pro MOTU Audio Express 
  hide details  
Reply
post #3 of 13
Thread Starter 
Quote:
Originally Posted by Chipp View Post

Take a look at your DisplayAbb function:
Code:
string DisplayAbb(string Abb)
{
        Abb[0] = toupper(Abb[0]);
        Abb[1] = toupper(Abb[2]);
        return Abb;
}


I didn't run your code, but with a quick glance it would seem you're trying to uppercase a letter in the Abb string that does not exist (since all state abbreviations are two letters, and array indexing in C++ starts at 0).

Wow.. yup that fixed it. Simple mistake I should have seen that doh.gif

Thanks Chipp!
Power Edition
(18 items)
 
   
CPUMotherboardGraphicsRAM
i7 3610QM Clevo p151EMx Intel HM77 NVidia GTX 670m 16GB 1600Mhz DDR3 Samsung 4x4 
Hard DriveOptical DriveCoolingOS
Seagate Momentus 750GB 7200 RPM 16MB Cache TSSTcorp CDDVDW SN-208AB Sager NP9130 Internal Fans Windows 7 Ultimate 64 bit 
MonitorKeyboardPowerCase
15.6” 1920x1080 LED-Backlit Display Matte Finish Sager NP9130 Keyboard 8 Cell Smart Lithium-Ion Battery 76.96Wh Sager NP9130 
MouseAudio
Touchpad or Logitech G5 Onkyo Installed Speakers 
  hide details  
Reply
Power Edition
(18 items)
 
   
CPUMotherboardGraphicsRAM
i7 3610QM Clevo p151EMx Intel HM77 NVidia GTX 670m 16GB 1600Mhz DDR3 Samsung 4x4 
Hard DriveOptical DriveCoolingOS
Seagate Momentus 750GB 7200 RPM 16MB Cache TSSTcorp CDDVDW SN-208AB Sager NP9130 Internal Fans Windows 7 Ultimate 64 bit 
MonitorKeyboardPowerCase
15.6” 1920x1080 LED-Backlit Display Matte Finish Sager NP9130 Keyboard 8 Cell Smart Lithium-Ion Battery 76.96Wh Sager NP9130 
MouseAudio
Touchpad or Logitech G5 Onkyo Installed Speakers 
  hide details  
Reply
post #4 of 13
Quote:
Originally Posted by ShotgunBFFL View Post

Quote:
Originally Posted by Chipp View Post

Take a look at your DisplayAbb function:
Code:
string DisplayAbb(string Abb)
{
        Abb[0] = toupper(Abb[0]);
        Abb[1] = toupper(Abb[2]);
        return Abb;
}


I didn't run your code, but with a quick glance it would seem you're trying to uppercase a letter in the Abb string that does not exist (since all state abbreviations are two letters, and array indexing in C++ starts at 0).

Wow.. yup that fixed it. Simple mistake I should have seen that doh.gif

Thanks Chipp!

No problem! Sometimes it is the most simple mistakes that are the hardest to find... a fresh set of eyes can be a huge resource when debugging.
Morpheus Mini
(7 items)
 
  
CPUMotherboardGraphicsRAM
i7-6700T SN970 GTX 960 Crucial CT102464BF160B 
Hard DriveOSAudio
Samsung 850 Evo M.2 Windows 10 Pro MOTU Audio Express 
  hide details  
Reply
Morpheus Mini
(7 items)
 
  
CPUMotherboardGraphicsRAM
i7-6700T SN970 GTX 960 Crucial CT102464BF160B 
Hard DriveOSAudio
Samsung 850 Evo M.2 Windows 10 Pro MOTU Audio Express 
  hide details  
Reply
post #5 of 13
Thread Starter 
I hate to reopen this topic, but somehow I'm getting the same error now later on in the program. I fixed the part that Chipp found and now it runs until I enter Y into my do while loop to loop it again. I'm lost as to what can be the problem because it doesn't really point out what part is failing.
Power Edition
(18 items)
 
   
CPUMotherboardGraphicsRAM
i7 3610QM Clevo p151EMx Intel HM77 NVidia GTX 670m 16GB 1600Mhz DDR3 Samsung 4x4 
Hard DriveOptical DriveCoolingOS
Seagate Momentus 750GB 7200 RPM 16MB Cache TSSTcorp CDDVDW SN-208AB Sager NP9130 Internal Fans Windows 7 Ultimate 64 bit 
MonitorKeyboardPowerCase
15.6” 1920x1080 LED-Backlit Display Matte Finish Sager NP9130 Keyboard 8 Cell Smart Lithium-Ion Battery 76.96Wh Sager NP9130 
MouseAudio
Touchpad or Logitech G5 Onkyo Installed Speakers 
  hide details  
Reply
Power Edition
(18 items)
 
   
CPUMotherboardGraphicsRAM
i7 3610QM Clevo p151EMx Intel HM77 NVidia GTX 670m 16GB 1600Mhz DDR3 Samsung 4x4 
Hard DriveOptical DriveCoolingOS
Seagate Momentus 750GB 7200 RPM 16MB Cache TSSTcorp CDDVDW SN-208AB Sager NP9130 Internal Fans Windows 7 Ultimate 64 bit 
MonitorKeyboardPowerCase
15.6” 1920x1080 LED-Backlit Display Matte Finish Sager NP9130 Keyboard 8 Cell Smart Lithium-Ion Battery 76.96Wh Sager NP9130 
MouseAudio
Touchpad or Logitech G5 Onkyo Installed Speakers 
  hide details  
Reply
post #6 of 13
Quote:
Originally Posted by ShotgunBFFL View Post

I hate to reopen this topic, but somehow I'm getting the same error now later on in the program. I fixed the part that Chipp found and now it runs until I enter Y into my do while loop to loop it again. I'm lost as to what can be the problem because it doesn't really point out what part is failing.
Is it the exact same error message? What line does your debugger point to when the exception occurs?
    
CPUMotherboardGraphicsGraphics
FX-8350 Asus Crosshair V MSI GTX460 Hawk 1gb MSI GTX460 Hawk 1gb 
RAMHard DriveHard DriveHard Drive
Kingston HyperX 8gb (2x4gb) Crucial M4 64GB Samsung F3 1TB Western Digital 320GB 
CoolingOSMonitorMonitor
Custom WC Windows 7 Ultimate X64 Dell E2311H Dell E2311H 
MonitorKeyboardPowerCase
LH 23EN43 Ducky Year of the Dragon 2012 SilverStone Strider 1000W-P Corsair 800D 
Audio
Asus Xonar Essence STX 
  hide details  
Reply
    
CPUMotherboardGraphicsGraphics
FX-8350 Asus Crosshair V MSI GTX460 Hawk 1gb MSI GTX460 Hawk 1gb 
RAMHard DriveHard DriveHard Drive
Kingston HyperX 8gb (2x4gb) Crucial M4 64GB Samsung F3 1TB Western Digital 320GB 
CoolingOSMonitorMonitor
Custom WC Windows 7 Ultimate X64 Dell E2311H Dell E2311H 
MonitorKeyboardPowerCase
LH 23EN43 Ducky Year of the Dragon 2012 SilverStone Strider 1000W-P Corsair 800D 
Audio
Asus Xonar Essence STX 
  hide details  
Reply
post #7 of 13
Thread Starter 
Quote:
Originally Posted by FiX View Post

Is it the exact same error message? What line does your debugger point to when the exception occurs?

I'm not sure because it's not a build error. It builds and I run it and the first loop works perfectly (I entered California and it returned CA) then I type y to continue the loop and then a window pops up saying Debug Assertion Failed!
There's a bunch of info on there but the expression says string subscript out of range. If I hit "Retry" to debug the application it says project has triggered a breakpoint but it doesn't point to any specific line in my code.

Edit: Actually it says line 1441 unsigned int.
Edited by ShotgunBFFL - 2/27/13 at 9:38pm
Power Edition
(18 items)
 
   
CPUMotherboardGraphicsRAM
i7 3610QM Clevo p151EMx Intel HM77 NVidia GTX 670m 16GB 1600Mhz DDR3 Samsung 4x4 
Hard DriveOptical DriveCoolingOS
Seagate Momentus 750GB 7200 RPM 16MB Cache TSSTcorp CDDVDW SN-208AB Sager NP9130 Internal Fans Windows 7 Ultimate 64 bit 
MonitorKeyboardPowerCase
15.6” 1920x1080 LED-Backlit Display Matte Finish Sager NP9130 Keyboard 8 Cell Smart Lithium-Ion Battery 76.96Wh Sager NP9130 
MouseAudio
Touchpad or Logitech G5 Onkyo Installed Speakers 
  hide details  
Reply
Power Edition
(18 items)
 
   
CPUMotherboardGraphicsRAM
i7 3610QM Clevo p151EMx Intel HM77 NVidia GTX 670m 16GB 1600Mhz DDR3 Samsung 4x4 
Hard DriveOptical DriveCoolingOS
Seagate Momentus 750GB 7200 RPM 16MB Cache TSSTcorp CDDVDW SN-208AB Sager NP9130 Internal Fans Windows 7 Ultimate 64 bit 
MonitorKeyboardPowerCase
15.6” 1920x1080 LED-Backlit Display Matte Finish Sager NP9130 Keyboard 8 Cell Smart Lithium-Ion Battery 76.96Wh Sager NP9130 
MouseAudio
Touchpad or Logitech G5 Onkyo Installed Speakers 
  hide details  
Reply
post #8 of 13
Thread Starter 
double post oops
Power Edition
(18 items)
 
   
CPUMotherboardGraphicsRAM
i7 3610QM Clevo p151EMx Intel HM77 NVidia GTX 670m 16GB 1600Mhz DDR3 Samsung 4x4 
Hard DriveOptical DriveCoolingOS
Seagate Momentus 750GB 7200 RPM 16MB Cache TSSTcorp CDDVDW SN-208AB Sager NP9130 Internal Fans Windows 7 Ultimate 64 bit 
MonitorKeyboardPowerCase
15.6” 1920x1080 LED-Backlit Display Matte Finish Sager NP9130 Keyboard 8 Cell Smart Lithium-Ion Battery 76.96Wh Sager NP9130 
MouseAudio
Touchpad or Logitech G5 Onkyo Installed Speakers 
  hide details  
Reply
Power Edition
(18 items)
 
   
CPUMotherboardGraphicsRAM
i7 3610QM Clevo p151EMx Intel HM77 NVidia GTX 670m 16GB 1600Mhz DDR3 Samsung 4x4 
Hard DriveOptical DriveCoolingOS
Seagate Momentus 750GB 7200 RPM 16MB Cache TSSTcorp CDDVDW SN-208AB Sager NP9130 Internal Fans Windows 7 Ultimate 64 bit 
MonitorKeyboardPowerCase
15.6” 1920x1080 LED-Backlit Display Matte Finish Sager NP9130 Keyboard 8 Cell Smart Lithium-Ion Battery 76.96Wh Sager NP9130 
MouseAudio
Touchpad or Logitech G5 Onkyo Installed Speakers 
  hide details  
Reply
post #9 of 13
Try this:
Code:
string DisplayAbb(string Abb)
{
        if(Abb.length() < 3)
                return Abb;

        Abb[0] = toupper(Abb[0]);
        Abb[1] = toupper(Abb[2]);
        return Abb;
}
    
CPUMotherboardGraphicsGraphics
FX-8350 Asus Crosshair V MSI GTX460 Hawk 1gb MSI GTX460 Hawk 1gb 
RAMHard DriveHard DriveHard Drive
Kingston HyperX 8gb (2x4gb) Crucial M4 64GB Samsung F3 1TB Western Digital 320GB 
CoolingOSMonitorMonitor
Custom WC Windows 7 Ultimate X64 Dell E2311H Dell E2311H 
MonitorKeyboardPowerCase
LH 23EN43 Ducky Year of the Dragon 2012 SilverStone Strider 1000W-P Corsair 800D 
Audio
Asus Xonar Essence STX 
  hide details  
Reply
    
CPUMotherboardGraphicsGraphics
FX-8350 Asus Crosshair V MSI GTX460 Hawk 1gb MSI GTX460 Hawk 1gb 
RAMHard DriveHard DriveHard Drive
Kingston HyperX 8gb (2x4gb) Crucial M4 64GB Samsung F3 1TB Western Digital 320GB 
CoolingOSMonitorMonitor
Custom WC Windows 7 Ultimate X64 Dell E2311H Dell E2311H 
MonitorKeyboardPowerCase
LH 23EN43 Ducky Year of the Dragon 2012 SilverStone Strider 1000W-P Corsair 800D 
Audio
Asus Xonar Essence STX 
  hide details  
Reply
post #10 of 13
Thread Starter 
Now it just returns Ca and ignores the capitalization. I'm pretty sure its a problem with the loop in my main.
Power Edition
(18 items)
 
   
CPUMotherboardGraphicsRAM
i7 3610QM Clevo p151EMx Intel HM77 NVidia GTX 670m 16GB 1600Mhz DDR3 Samsung 4x4 
Hard DriveOptical DriveCoolingOS
Seagate Momentus 750GB 7200 RPM 16MB Cache TSSTcorp CDDVDW SN-208AB Sager NP9130 Internal Fans Windows 7 Ultimate 64 bit 
MonitorKeyboardPowerCase
15.6” 1920x1080 LED-Backlit Display Matte Finish Sager NP9130 Keyboard 8 Cell Smart Lithium-Ion Battery 76.96Wh Sager NP9130 
MouseAudio
Touchpad or Logitech G5 Onkyo Installed Speakers 
  hide details  
Reply
Power Edition
(18 items)
 
   
CPUMotherboardGraphicsRAM
i7 3610QM Clevo p151EMx Intel HM77 NVidia GTX 670m 16GB 1600Mhz DDR3 Samsung 4x4 
Hard DriveOptical DriveCoolingOS
Seagate Momentus 750GB 7200 RPM 16MB Cache TSSTcorp CDDVDW SN-208AB Sager NP9130 Internal Fans Windows 7 Ultimate 64 bit 
MonitorKeyboardPowerCase
15.6” 1920x1080 LED-Backlit Display Matte Finish Sager NP9130 Keyboard 8 Cell Smart Lithium-Ion Battery 76.96Wh Sager NP9130 
MouseAudio
Touchpad or Logitech G5 Onkyo Installed Speakers 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Application Programming