Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > project1

Reply
Thread Tools

project1

 
 
MC felon
Guest
Posts: n/a
 
      12-12-2006
hi.
i've been trying to make this (simple but simply infruriating) project.
i've recently walked into STANDARD C++ and was working in Turbo all
this while. As a result, i'm not accustomed to the new lib. functions
of Gnu. here's my code for the app.(incidentally, i'm trying to make
the app code and decode letters.... like, store 'a' as 'j' and 'b' as
something else) however, when i compile it (on code::blocks), it says:

"this application has requested the runtime to terminate it in an
unusual way. Please contact the application's support team for more
information"

whats wrong?

#include <iostream>
#include <conio.h>
#include <string>
int main()
{
std::cout<<"rough engine of ecod\n\n";
std::cout<<"please input text\n\n";
std::string str;
int number_count = 0;
std::string out;
getline(std::cin,str);
str.push_back('\t');
while( str.at(number_count) != '\t')
{
number_count++;
}
for(int l = 0;l<number_count;l++)
{
if(str.at(l) == '\0')
{
out.at(l) = 'j';
}
if(str.at(l) == '.')
{
out.at(l) = 'k';
}
if(str.at(l) == 'a')
{
out.at(l) = '*';
}
if(str.at(l) == 'b')
{
out.at(l) = '-';
}
if(str.at(l) == 'c')
{
out.at(l) = 'l';
}
if(str.at(l) == 'd')
{
out.at(l) = 'm';
}
if(str.at(l) == 'e')
{
out.at(l) = 'n';
}
if(str.at(l) == 'f')
{
out.at(l) = 'p';
}
if(str.at(l) == 'g')
{
out.at(l) = 'q';
}
if(str.at(l) == 'h')
{
out.at(l) = 'r';
}
if(str.at(l) == 'i')
{
out.at(l) = 's';
}
if(str.at(l) == 'j')
{
out.at(l) = 't';
}
if(str.at(l) == 'k')
{
out.at(l) = 'u';
}
if(str.at(l) == 'l')
{
out.at(l) = 'v';
}
if(str.at(l) == 'm')
{
out.at(l) = 'w';
}
if(str.at(l) == 'n')
{
out.at(l) = 'x';
}
if(str.at(l) == 'o')
{
out.at(l) = 'y';
}
if(str.at(l) == 'p')
{
out.at(l) = 'z';
}
if(str.at(l) == 'q')
{
out.at(l) = 'a';
}
if(str.at(l) == 'r')
{
out.at(l) = 'b';
}
if(str.at(l) == 's')
{
out.at(l) = 'c';
}
if(str.at(l) == 't')
{
out.at(l) = 'd';
}
if(str.at(l) == 'u')
{
out.at(l) = 'e';
}
if(str.at(l) == 'v')
{
out.at(l) = 'f';
}
if(str.at(l) == 'w')
{
out.at(l) = 'g';
}
if(str.at(l) == 'x')
{
out.at(l) = 'h';
}
if(str.at(l) == 'y')
{
out.at(l) = 'i';
}
if(str.at(l) == 'z')
{
out.at(l) = 'i';
}
}
std::cout<<"\n\n";
std::cout<<"here's the coded version...\n\n";
std::cout<<out;
getch();
}

 
Reply With Quote
 
 
 
 
Evan
Guest
Posts: n/a
 
      12-12-2006

MC felon wrote:
> hi.
> i've been trying to make this (simple but simply infruriating) project.
> i've recently walked into STANDARD C++ and was working in Turbo all
> this while. As a result, i'm not accustomed to the new lib. functions
> of Gnu. here's my code for the app.(incidentally, i'm trying to make
> the app code and decode letters.... like, store 'a' as 'j' and 'b' as
> something else) however, when i compile it (on code::blocks), it says:
>
> "this application has requested the runtime to terminate it in an
> unusual way. Please contact the application's support team for more
> information"
>
> whats wrong?
>
> #include <iostream>
> #include <conio.h>


Just a note... conio (and getch) isn't standard C++. Some around here
will jump on you for that. [Ironic, don't you think, that I'm saying
that?]

> #include <string>
> int main()
> {
> std::cout<<"rough engine of ecod\n\n";
> std::cout<<"please input text\n\n";
> std::string str;
> int number_count = 0;
> std::string out;
> getline(std::cin,str);
> str.push_back('\t');
> while( str.at(number_count) != '\t')
> {
> number_count++;
> }


You're using number_count to count the number of characters in the
string. But you have access to that easily: str.size() or str.length().
No need to use that loop, and no need to add the \t sentinel to the end
of the string.

(BTW, strings always end with the null character '\0', and I think that
str.at() will let you access it, so you can just look for that instead
adding and looking for \t.)

> for(int l = 0;l<number_count;l++)
> {
> if(str.at(l) == '\0')
> {
> out.at(l) = 'j';
> }
> if(str.at(l) == '.')
> {
> out.at(l) = 'k';
> }
> if(str.at(l) == 'a')
> {
> out.at(l) = '*';
> }

[snip]

Look into the case statement, or try to figure out a better way

> if(str.at(l) == 'z')
> {
> out.at(l) = 'i';
> }
> }
> std::cout<<"\n\n";
> std::cout<<"here's the coded version...\n\n";
> std::cout<<out;
> getch();
> }


Now, the problem is that out is a zero length string. When you try to
do out.at(1) = (whatever) for the first character, that's out of bounds
for out, and because you're using at() instead of [], it throws an
exception. Your program doesn't catch it, so the runtime calls
terminate to end your program. (BTW, [] wouldn't help your situation...
with that you'd probably crash your program in another way.)

You either need to make out big enough that all the indices will be in
bounds to start with (with resize() or the constructor that takes an
int -- if strings have them, I forget now) or use push_back to add
something to the end.

Good luck!
Evan

 
Reply With Quote
 
 
 
 
MC felon
Guest
Posts: n/a
 
      12-12-2006

> You either need to make out big enough that all the indices will be in
> bounds to start with (with resize() or the constructor that takes an
> int -- if strings have them, I forget now) or use push_back to add
> something to the end.
>
> Good luck!
> Evan


so what's the syntax for resize()? or should i try:

std::string out(1000); // constructor

 
Reply With Quote
 
MC felon
Guest
Posts: n/a
 
      12-12-2006

MC felon wrote:
> > You either need to make out big enough that all the indices will be in
> > bounds to start with (with resize() or the constructor that takes an
> > int -- if strings have them, I forget now) or use push_back to add
> > something to the end.
> >
> > Good luck!
> > Evan

>
> so what's the syntax for resize()? or should i try:
>
> std::string out(1000); // constructor


NO! dont bother answering that last. THANKS a lot for sparing your
time..... i made my first project!!! it works...

i just tried:
std::string out;
out.resize(1000);


it works!!! thanks a lot again!

 
Reply With Quote
 
eriwik@student.chalmers.se
Guest
Posts: n/a
 
      12-12-2006
On Dec 12, 6:01 am, "MC felon" <(E-Mail Removed)> wrote:
> MC felon wrote:
> > > You either need to make out big enough that all the indices will be in
> > > bounds to start with (with resize() or the constructor that takes an
> > > int -- if strings have them, I forget now) or use push_back to add
> > > something to the end.

>
> > > Good luck!
> > > Evan

>
> > so what's the syntax for resize()? or should i try:

>
> > std::string out(1000); // constructor

> NO! dont bother answering that last. THANKS a lot for sparing your
> time..... i made my first project!!! it works...
>
> i just tried:
> std::string out;
> out.resize(1000);


A better way would be out.resize(str.size()); which will make out just
the size you need.

--
Erik Wikström

 
Reply With Quote
 
eriwik@student.chalmers.se
Guest
Posts: n/a
 
      12-12-2006
On Dec 12, 5:15 am, "MC felon" <(E-Mail Removed)> wrote:
> hi.
> i've been trying to make this (simple but simply infruriating) project.
> i've recently walked into STANDARD C++ and was working in Turbo all
> this while. As a result, i'm not accustomed to the new lib. functions
> of Gnu.


Just a note, it's not GNU-libraries, it's the C++ standard library. GNU
has made one implementation, but they are hardly alone.

--
Erik Wikström

 
Reply With Quote
 
I V
Guest
Posts: n/a
 
      12-12-2006
On Mon, 11 Dec 2006 20:27:58 -0800, Evan wrote:
> (BTW, strings always end with the null character '\0', and I think that
> str.at() will let you access it, so you can just look for that instead
> adding and looking for \t.)


Arrays of characters used to represent strings end with '\0'. std::strings
don't.
 
Reply With Quote
 
Jacek Dziedzic
Guest
Posts: n/a
 
      12-12-2006
MC felon wrote:
> MC felon wrote:
>>> You either need to make out big enough that all the indices will be in
>>> bounds to start with (with resize() or the constructor that takes an
>>> int -- if strings have them, I forget now) or use push_back to add
>>> something to the end.
>>>
>>> Good luck!
>>> Evan

>> so what's the syntax for resize()? or should i try:
>>
>> std::string out(1000); // constructor

>
> NO! dont bother answering that last. THANKS a lot for sparing your
> time..... i made my first project!!! it works...
>
> i just tried:
> std::string out;
> out.resize(1000);
>
>
> it works!!! thanks a lot again!


Does it? Try "encoding" 'o'. I'd say you'll get 'i', whereas
you expect 'y'. By using two strings, one for the plaintext alphabet
and one for the "encoded" alphabet you can get rid of all these
if statements, and, as a bonus, easily fix this bug.

HTH,
- J.
 
Reply With Quote
 
BobR
Guest
Posts: n/a
 
      12-12-2006

MC felon wrote in message ...
>
>i just tried:
>std::string out;
>out.resize(1000);


std::string out( 1000, '\0' ); // fill with zeros
or,
std::string out( 1000, ' ' ); // fill with spaces


std::string in( "fill it up with something" );
std::string out( in.size(), ' ' ); // fill with spaces

--
Bob R
POVrookie


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off




Advertisments