Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I tried to write lastindexOf function for my C++ class. After I was trying for 2 weeks, I still can't get it working.
At first , I was trying to follow the logic from this post: CString find the last entry , but since they use CString class instead of char class,I have no success to duplicate the code for char class. I also try the strstr, but I have no luck with that neither. I would appreciate any helps.
here is the code I have came up with so far :
#include
using namespace std;
int lastIndexOf(char *s, char target);
int main()
{
char input[50];
cin.getline(input, 50);
char h = h;
lastIndexOf(input, h);
return 0;
}
int lastIndexOf( char *s, char target)
{
int result = -1;
while (*s != '\0')
{
if (*s == target ){
return *s;
}}
return result;
}
Try this:
int lastIndexOf(const char * s, char target)
{
int ret = -1;
int curIdx = 0;
while(s[curIdx] != '\0')
{
if (s[curIdx] == target) ret = curIdx;
curIdx++;
}
return ret;
}
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 7 days ago.
Improve this question
So I have this strcpy with tables but I need to change it so that there's no tables and only pointers. When I try to do it, there's an error (I put $$ in front)
So the original:
#include <iostream>
using namespace std;
int main() {
char *mon_strcpy(char destination[], char source[]) {
int index = 0;
while (source[index] != '\0') {
destination[index] = source[index];
index++;
}
destination[index] = '\0';
return destination;
}
return 0;
}
And this is the one I'm trying to make it work:
#include <iostream>
using namespace std;
int main() {
char *mon_strcpy(char *destination, char *source) $${
int index = 0;
while (*source != '\0')
{
*destination = *source;
index++;
}
*destination = '\0';
return destination;
}
return 0;
}
I can't wrap my head around to find the problem.. TIA
In C & C++, you have to declare-define a function outside the other function (here main()). Something like:
char *mon_strcpy(char *destination, char *source) { ... }
int main () {
mon_strcpy(dst, src);
}
Also $$ sign is not allowed to be used inside C++ code except comments.
This just addresses the compiler error you have.
If you have a problem with the function logic, why is it not working? You may want to debug followed by a new question.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 months ago.
Improve this question
Let’s say I have 128 and I broke it up into 1, 2 and 8.
Can anyone give me a logic to build the number again from its broken digits.
If you have a vector of ints, you can do:
#include <vector>
#include <cstdio>
#include <cstdlib>
int to_integer( const std::vector<int>& v ) {
int number = 0;
for ( int value : v ) {
number = 10*number + value;
}
return number;
}
int main() {
std::vector<int> vec {1,2,8};
int number = to_integer( vec );
printf( "Number:%d\n", number );
return 0;
}
If it was a string, you could simply use the C library
#include <cstdlib>
...
const char* str = "128";
int number = ::atoi( str );
But that's likely not what you are asking.
You can do the hands-on approach
#include <cstdio>
int to_integer( const char* str ) {
int number = 0;
for ( ; *str != '\0'; str++ ) {
char ch = *str;
number = 10*number + (ch-'0');
}
return number;
}
int main() {
const char* str = "128";
int number = to_integer( str );
printf( "Number:%d\n", number );
return 0;
}
Please note that this routine above is just a minimal, simplistic and it does not check for cases that the C library does as eg: non-numeric charaters, white spaces, null pointer.
However many times we can guarantee all above as a precondition so the above becomes valid production code. I actually use something like that for high speed trading.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 years ago.
Improve this question
i'm trying to make a very simple program with stacks but i seem to be getting an error when i try to run it. The error says "ISO C++ forbids comparison between pointer and integer.". Any help is greatly appreciated.
My Code:
#include <iostream>
#include <string>
using namespace std;
const int maxstack = 5;
struct stacktype{
string name[maxstack];
int top;
};
void createstack(stacktype &stack);
void destroystack(stacktype &stack);
bool fullstack(stacktype stack);
void push(stacktype &stack, string &newelement);
bool emptystack(stacktype stack);
void pop(stacktype &stack, string &poppedelement);
int main(){
stacktype stack;
string newelement, poppedelement;
char quest;
createstack(stack);
cout<<"Do you want to enter data? (y/n)";
cin>>quest;
while((quest == "y" || quest == "Y") && !(fullstack(stack))){ //I get the error on this line
cout<<"Please enter name";
cin>>newelement;
push(stack, newelement);
cout<<"Do you want to enter data? (y/n)";
cin>>quest;
}
cout<<endl<<endl;
while(!emptystack(stack)){
pop(stack, poppedelement);
cout<<poppedelement<<endl;
}
destroystack(stack);
system("PAUSE");
return 0;
}
void createstack(stacktype &stack){
stack.top = -1;
}
void destroystack(stacktype &stack){
stack.top = -1;;
}
bool fullstack(stacktype stack){
if(stack.top == maxstack - 1){
return true;
}else{
return false;
}
}
void push(stacktype &stack, string &newelement){
stack.top++;
stack.name[stack.top] = newelement;
}
bool emptystack(stacktype stack){
if(stack.top == -1){
return true;
}else{
return false;
}
}
void pop(stacktype &stack, string &poppedelement){
poppedelement = stack.name[stack.top];
stack.top--;
}
quest is a char, yet "y" is a string literal with type const char[2]. When you try to compare these with quest == "y", the string literal is converted to a pointer to its first element, and so you are attempting to compare a char with a pointer. That's what the error is telling you.
Instead of a string literal, you want a character literal like 'y', which has type char.
quest is of char type, so it is single character, and you try to compare it with string literals like "y". Use single quotes for char literals: 'y'.
Since you are writing C++ code, you should use the string class instead of "y".
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I don't understand why this won't run properly in Visual Studio 2012
#include <iostream>
#include <cstring>
#include <cstdlib>
using namespace std;
char nullChar()
{
char ch;
int ran = (rand() % 52);
if (ran < 26)
{
ch = (char) ('a'+ ran);
}
else
{
ch = (char) ('A'+ ran - 26);
}
return ch;
}
int main(int argc, char *argv[])
{
cout << "Enter a string";
int nullNum = 0;
nullNum = atoi(argv[2]);
if (strcmp(argv[1], "-d") == 0)
{
int count = -1;
do
{
int c = cin.get();
count++;
if(count == nullNum)
{
cout.put(c);
count = -1;
}
} while (!cin.eof()) ;
}
if(strcmp(argv[1], "-e") == 0)
{
char c = cin.get();
while(!cin.eof())
{
for (int i = -1; i < (nullNum-1); ++i)
{
cout << nullChar();
}
cout << c;
c = cin.get();
}
}
}
The code compiles perfectly. I'm suspecting something in a loop but I can't figure it out. I also think it ran perfectly a few days ago but now it's not. Is that possible?
Im not sure if you passed a parameter. But aside from that you should always check argc before using argv[xyz]. My guess is you get a segfault, because there is no argv[1] and argv[2]
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a very useful code bit in matlab.
I am using this code bit to save files in different parts of my code, without overwrite existing ones.
Can someone please guide me how to translate this code to C/C++ ?
i=0;
name= ['test_', int2str(i)];
while exist(name)
i=i+1;
name= ['test_', int2str(i)];
end
save(name)
In C++ on Windows I'd use something like :
#include <iostream>
#include<fstream>
#include<string>
#include<sstream>
template <typename T>
std::string num2str ( T Number )
{
std::stringstream ss;
ss << Number;
return ss.str();
}
inline bool if_exists (const std::string& name) {
std::ifstream f(name.c_str());
if (f.good()) {
f.close();
return true;
} else {
f.close();
return false;
}
}
std::string get_next_file( void )
{
int i=1;
while (if_exists("test_" + num2str(i) ) )
i++;
return std::string("test_") + num2str(i);
}