if statement doesn't work with function [closed] - c++

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 9 years ago.
Improve this question
void skaitoInformacija(){
ifstream duomenys("duom.txt");
int eil_nr;
duomenys >> eil_nr;
string eil[eil_nr];
string nereikalinga_eilute;
getline(duomenys, nereikalinga_eilute);
for(int i=0; i<eil_nr; i++){
getline(duomenys, eil[i]);
if(salinamTarpus(eil[i]) == "good"){ //this if statement doesn't work
}
}
}
void salinamTarpus(string eil) {
...
}

void salinamTarpus(string eil)
your function is not returning anything that you can compare with "good" string
you need to change it to return at least some result if you want to compare it...
string salinamTarpus(string eil) {
if(eil == "okString") // string eil is the right one
{
return "good";
}
return "bad";
}
also if your function salinamTarpus(string eil) returns only 2 values("good","bad") it might be better idea to return boolean,char or so. string is a little bit too much overkill

Related

How to decide is it a string or not? [closed]

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 10 months ago.
Improve this question
I have an array of strings and I'd like to assort the elements by real datatype ("1" -> int; "abc" -> string; "1a" -> string) :
#include <iostream>
#include<vector>
int main() {
std::vector<std::string> number;
std::vector<std::string> str;
std::string arr[5] = {"1","1a","ab","10.1","a2"};
for(int i = 0;i<5;i++){
if(arr[i] /* is string */){
str.push_back(arr[i]);
}
else if(arr[i] /* is int */){
number.push_back(arr[i]);
}
}
return 0;
}
What is the simplest way?
Thanks!
If your goal is to only examine whether a string contains integer/decimal or not, you can try a solution with regexes:
if(std::regex_match(arr[i], std::regex("[-|+]?[0-9]*[.,]?[0-9]+")))
number.push_back(arr[i]);
else
str.push_back(arr[i]);
I'm assuming you're not taking overflow into account because you're only using strings, otherwise if you wanted to convert strings to numbers, you'd have to take that into account and use other solutions.

memoizing a recursion solution (DP) [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I have written this recursion for the problem https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-transaction-fee
I want to know how can we memoize this solution (using a dp array).
or do we have to write recursion in a specific way to memoize it?
class Solution {
public:
int solve(vector<int>& prices,int fee,int i,int profit,int buy,int sell)
{
if(i==prices.size())
{
if(buy==sell)
return profit;
else
return 0;
}
int ans = 0;
ans = max(ans,solve(prices,fee,i+1,profit,buy,sell));
if(buy>sell)
{
ans = max(ans,solve(prices,fee,i+1,profit+prices[i]-fee,buy,sell+1));
}
else
{
ans = max(ans,solve(prices,fee,i+1,profit-prices[i],buy+1,sell));
}
return ans;
}
int maxProfit(vector<int>& prices, int fee) {
vector<int> diff;
int sum = 0;
sum = solve(prices,fee,0,0,0,0);
return sum;
}
};
You can just create an array where element i is equal to solve(i). Then, inside your function, you can pass this array through by reference to each call. You add an if/else structure in your function testing if the input you got was defined in the array, if so return arr[input] and if not, run through your normal function except just before you return, you initialize arr[input] to the value you will return.

how void function can be return value? c++ [closed]

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 6 years ago.
Improve this question
I do not understand, how void* function can be with return value. Code below, its work.
void *TcpClient::receive(int size=512)
{
char *buffer = new char[size];
if (recv(_sockfd , buffer , sizeof(buffer) , 0) < 0)
{
std::cerr << "recv failed";
}
return buffer;
}
The function returns void*, i.e. the pointer to memory, not void.

C++: Algorithm wrong output ( works on java ) [closed]

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 8 years ago.
Improve this question
Can someone help me with my code ? I'm trying to use different algorithm but it returns way to big numbers, when i use algorithm between /* */ it works perfect, anyone can see whats wrong with my new code ? (same on java works)
int* czynnikiPierwsze(int n)throw (string){
if(n<0){
string wyjatek1="Nie mozna rozlozyc ujemnej liczby";
throw wyjatek1;
}
int b=0;
while(n>2){
n=n/tab[n-2];
b++;
}
dzielniki=new int[b]();
int j=0;
while(n>2){
dzielniki[j]=tab[n-2];
n=n/tab[n-2];
j++;
}
/* int a=n;
int*dzielniki=new int[30]();
for(int j=0;j<n+a;j++){
while(n>2){
dzielniki[j]=tab[n-2];
n=n/tab[n-2];
break;
}
}*/
return dzielniki;
}
There's no chance your second while(n>2) loop runs even once, since the first loop only exited when that same condition was no longer true.

Threshoding image between certain range [closed]

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 9 years ago.
Improve this question
How to Threshold image between certain range?
i have done this but it doesn't work.
for (int i=0;i<s.size().height;i++)
{
for(int j=0;j<s.size().width;j++)
{
int k=int (s.at<uchar>(j,i));
if (k>6 && k<10)
k=255;
else k=0;
s.at<uchar>(j,i)=k;
}
}
You get an uchar value, and convert it to integer. Try this :
uchar k= s.at<uchar>(j,i);
if (k>6 && k<10) {
k=255;
}else {
k=0;
}
s.at<uchar>(j,i)=k;
I think it may work.