project1

M

MC felon

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();
}
 
E

Evan

MC said:
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
 
M

MC felon

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
 
M

MC felon

MC said:
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!
 
E

eriwik

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

eriwik

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

I V

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

Jacek Dziedzic

MC said:
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.
 
B

BobR

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,527
Members
44,998
Latest member
MarissaEub

Latest Threads

Top