The program itself works correctly, does what it's supposed to (seperate the words in a sentence and print them out) and does not crash. However, i cannot exit from the program. It just gets stuck. I even tried giving an exit(0) in the end but it didn't work.
Can you please tell me what's wrong?
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<ctype.h>
#include<string.h>
#include<process.h>
typedef char* string;
void main()
{
clrscr();
string s;
cout << "\nEnter something : ";
gets(s);
int i;
for (i = 0; i < strlen(s); ++i)
{
if ( s[i] != 32 )// && ( !isalnum(s[i-1]) || i == 0 ) )
{
char *word = s;
int end = 0;
for (; s[i] != 32 && i < strlen(s); ++i);
if (i == strlen(s)) end = 1;
else * (word + i) = '\0';
cout << "\n" << word;
if (end) break;
strcpy(s, s+i+1);
i = -1;
}
}
}
Undefined Behavior
You declare a pointer and don't initialize it (you don't make it point to anything):
string s;
// a.k.a. char * s;
Next, you input into it:
gets(string);
This is known as undefined behavior: writing to an unknown address. A nice operating system and platform would segfault.
In computer programming, you need to allocate memory either by using an array:
char s[256];
or dynamic allocation:
string s = new char[256];
before you put values in, either from input or elsewhere.
You told it to do that. Remove system("pause");
And, please, stop using the C library, and headers/tools from the 1980s. We've moved on from MS DOS in the intervening time. If you want marketable skills, learn actual ISO C++ (which was invented in 1998, and has been updated three times in the nearly two decades since then).
Related
Could someone explain to me, why there are 4 additional slots in char tab[], when I asked only for 3? How to get rid of them? I'm coding in Visual Studio 2017. Edit: the first program was very basic and didn't show what I intended. So, there is an extended one.
#include <iostream>
#include <vector>
using namespace std;
int main()
{
int i, n;
vector<char> input;
char chp;
cout << "Enter a expression" << endl;
while (1)
{
cin.get(chp);
if (chp == '\n') break;
input.push_back(chp);
}
n = input.size();
char* tab = new char[n] {};
for (i = 0; i < n; i++)
{
tab[i] = input[i];
}
int l = strlen(tab);
for (int i = 0; i < l; i++)
{
cout << "tab[" << i << "] is " << tab[i] << endl;
}
cin.get();
}
Result in console window is similar, when I enter "3+3"
tab[0] is 3
tab[1] is +
tab[2] is 3
tab[3] is ř
tab[4] is ř
tab[5] is ř
tab[6] is ř
This isn't still the full program (full program is a calculator, that calculates any math expression, and is much longer). I wrote that in C long time ago, and in C dynamic arrays are not such a problem.
Also, what about multidimensional arrays? Can string be a solution also for them?
Could someone explain to me, why there are 4 additional slots in char tab[], when I asked only for 3?
There aren't. The array has only 3 elements.
The problem is that your array elements have indeterminate values. As a consequence of passing a pointer to array of indeterminate values into strlen, the behaviour of your program is undefined.
Solution: Initialise your array. Furthermore, initialise it so that it contains a null terminator, as required by strlen:
char* tab = new char[3]{'a', 'b', '\0'};
As alternative to null termination, don't use strlen to get the length. You already know that the array contains 3 elements. But the values must still be initialised before you insert them into the output stream.
P.S. Don't forget to delete memory that you allocate:
delete[] tab;
I am trying to create a keygen to some reversing challenges around there.
I decided to try to code it in a language that I don't know well, c++
I not sure how to use pointers yet, but I thing that I implemented the algorithm well without them. the only problem is that I print the answer I get it's memory location instead all the string.
char decrypt(char c, int position);
int main(){
cout << "Enter username:" << endl;
string username;
string answer[20] = "";
cin >> username;
for (int i = username.length(); i > 0; i--){
answer[username.length() - i] = decrypt(username[i-1],i);
if (i == 0){
answer[username.length() +1] = '\0';
}
}
return 0;
}
char decrypt(char c, int position)
{
if (position == 4){
return '-'; }
if (c > 'M'){
c -= 17; }
else{
c += 21; }
c ^= position;
c ^= 2;
return c;
}
If I will try to print the string username I will get the string and not the memory location of username. So I'm not sure what is going on..
Thanks for any help,
Or
First, try to use the answer variable as a string and not a char*. A string authomatically resize and realocates its internal buffer if needed.
The code could looks like this:
string username;
string answer;
cin >> username;
for (int i = 0; i < username.length(); i++)
{
answer += decrypt(username[i],i+1);
}
Then if you want to see the content of the internal string buffer, you can use answer.c_str ();
Edit:
As songyuanyao said, your code uses an array of string. But the solution of using an array of 20 chars (char answer [20]) leads to a memory issue if the username has a size of 20 or more.
If I understand your question correctly,
string answer[20] = "";
should be
char answer[20];
string answer[20] is array of std::string, not the c-style string (i.e. char[]).
In this code, answer is not a string – it is an array of 20 strings. You probably want char answer[20], or, more likely, to create a single string answer and append the result with answer += character.
I am having trouble understanding why I am receiving an error here.
I assume it may have to do with trying to access a place that doesn't exist in the array? (I cant see where i'm doing that though)
When the program runs it will return the correct answer but then close with the runtime error. Would appreciate any help and general advice for avoiding these issues in the future. thanks
int len(char s[]){
int len = 0;
while (s[len] != 0) len++;
return len;
}
int isPalindorme(char s[]){
int length = len(s);
int limit = length / 2.0 + 0.5;
for (int i = 0; i < limit; i++){
if (s[i] != s[length - i -1]){
return 0;
}
}
return 1;
}
void main(){
char a[] = "";
cin >> a;
cout << len(a) << endl;
cout << isPalindorme(a);
}
a points to a buffer of only 1 byte in size (the null terminator), so the cin is trashing the stack when it copies data to that address.
You should use a std::string instead (or make your buffer a lot bigger).
I have just started to learn c++. I also want to clear that this is no homework question, its just something that I am stuck on.
I was going through assignment questions on the MIT website, and i have pasted the question for you here;
Write a function that returns the length of a string (char *), excluding the final NULL character. It should not use any standard-library functions. You may use arithmetic and dereference operators,but nottheindexing operator([]).
I don't know how to do this without an array.
Any help is appreciated!!
This is what i did:
#include<iostream>
#include<conio.h>
#include<string>
using namespace std;
int stringlength (char* numptr);
int main()
{
char *mystring;
cout<<"enter the string \n";
cin>>mystring;
cout<<"length is "<<stringlength(mystring);
getch();
}
int stringlength (char* numptr)
{
int count=0;
for(;*numptr<'\0';*numptr++)
{
count++;
}
return(count);
}
This is what i had done previously before I asked u all about the problem.
But this got me an answer of zero.
But if in my function i change *numptr<'\0' to *numptr!= 0, i get the right answer.
Now what i am confused about is, isn't that the null character, so why cant i check for that.
Since you are doing this as an educational thing, I'm not gonna give you the answer. But I will help you a little on the way.
Use a char* and the ++ operator to check for terminating zero \0 this will be the last character in the string.
First of all, this is not the way to learn C++ in 2013. The answer relies on low-level pointer manipulation. There are a lot more important things to learn about C++ before you get to this point. Right now, you should be learning about string, vector, functions, classes, and not about these low-level details.
To answer you question, you have to know how strings are represented. They are represented as an array of characters. In C and C++, arrays do not have a built in length. So you have to store it or use some other means of finding the length. The way the strings make is so you can find the length is that they store a 0, as the last position in the array. Thus "Hello" would be stored as
{'H','e','l','l','o',0}
To find the length you go through the array starting at index 0 and stop when you encounter a character value of 0;
The code would look something like this
int length(const char* str){
int i = 0;
for(; str[i] != 0; i++);
return i;
}
Now in C and C++ you can str[i] is the same as *(str + i);
So to satisfy your question you can write it like this
int length(const char* str){
int i = 0;
for(; *(str + i) != 0; i++);
return i;
}
Now, instead of using + i, you can increment str directly;
int length(const char* str){
int i = 0;
for(; *str++ != 0; i++){;
return i;
}
Now in C, a value is false if it is 0, otherwise it is true, so we do not need the != 0, so we can write
int length(const char* str){
int i = 0;
for(; *str++; i++){;
return i;
}
#include<iostream>
#include<conio.h>
#include<string>
using namespace std;
int stringlength (char* numptr);
int main()
{
char *mystring;
cout<<"enter the string \n";
cin>>mystring;
cout<<"length is "<<stringlength(mystring);
getch();
}
int stringlength (char* numptr)
{
int count=0;
for(;*numptr<0;*numptr++)
{
count++;
}
return(count);
}
I'm a beginner and i need to ask a question..
I wrote this small code that accepts a string from the user and prints it..very simple.
#include <iostream>
using namespace std;
int main()
{
int i;
char *p = new char[1];
for(i = 0 ; *(p+i) ; i++)
*(p+i) = getchar();
*(p+i) = 0;
for(i = 0 ; *(p+i) ; i++)
putchar(*(p+i));
return 0;
}
when i enter any string..like "stack overflow" for example..it will print "sta" and drop the rest of the string. I know it's an easy one to solve but since I've just started i can't understand what's wrong here . Thanks in advance .
There are several problems with this code. First, you have a buffer overflow, because char *p = new char[1] allocates only one character for storage. This is exceeded when i > 0. Next, your first loop will keep going until it reaches a point in unallocated memory (undefined behavior) that has a value of zero. This just happens to be after the third value in your case. You probably wanted something more like *(p+i-1) == 0 to give "the last character read meets some condition." Finally, you're allocating memory with new[] and not properly deallocating it with a matching delete[].
Consider using std::cin and std::string for much safer and correct code:
#include <iostream>
#include <string>
int main(int, char**) {
std::string s;
std::cout << "Enter a string: ";
std::cin >> s;
std::cout << s << std::endl;
}
Here is some code along your lines that seems to work. I'm sure there are better (and more C++-ish) ways to do this...
#include <iostream>
using namespace std;
#define MAXLEN 80
int main()
{
int i=0;
char c;
char *p = new char[MAXLEN + 1]; // 1 char will not be sufficient
do // Doing this with a for loop would be unreadable
{
c = getchar();
*(p+i) = c;
i++;
} while( c != '\n' && i < MAXLEN ); // Check for a newline. How do you enter the zero with a keyboard?
*(p+i) = 0; // Ensure that the last character is zero
for(i = 0 ; *(p+i) ; i++) putchar(*(p+i)); // This is OK but difficult to read
delete [] p; // Don't forget this
return 0;
}
The fact that your program does anything is just luck; what stops *(p+i) from being \0 to begin with? It's weird that you're using getchar() and putchar() in a C++ program, too. What's the story behind this program?
If you read into memory, be sure that you allocate enough. new char[1] creates an array of only one char, but you are reading more then that. A simple temporary fix would be to simply allocate more, say new char[255].
Other notes:
you never delete the memory you allocated: delete[] p;
you should check wether you read as much characters as your buffer can hold: for(..;.. && i<bufferSize;..)
the condition in the first loop always checks the next character, not what you just read
*(p+i) is equivalent to p[i], which is more readable
why read and write only one character at a time?
why not use iostreams (std::in, std::out) and std::string as you are using C++?
you only allocate space for one character but you try to put many chars in it.
Is this homework? if so please tag it as such. Are you allowed to use STL?
If so then use std::vector instead on new char[1];
EDIT:to do it without any fiddly bits or STL
const int MAX = 100;
char *p=new char[MAX];
for(i = 0 ; *(p+i) && i < MAX ; i++)
*(p+i) = getchar();
probably some out by ones - left as exercise