Ralph
Okay thankss
Ralph
So how can i fix it to read my if's
Ralph
I mean why non are working
Igor🇺🇦
I mean why non are working
What does it mean "non are working "?
H
I mean why non are working
we just saw your function we didnt see anything else about usage of function 😐
Ralph
Do you have a problem if i send it here?
Ralph
It's just 3 lines
Ralph
int main() { std::vector<int> v{ 1,18,12,11,10,8 }; int res = peak(v, 0, v.size() - 1); std::cout << res << "\n"; }
Ralph
The int peak is in my helper
Ralph
The code i sent above
Ralph
I'm trying the divide and conquer
Igor🇺🇦
I'm trying the divide and conquer
It doesn't matter what approach you're using to implement the wrong algorithm. What are you trying to calculate?
Ralph
I'm trying to calculate the peak
Igor🇺🇦
I'm trying to calculate the peak
Something like this https://codeforwin.org/2016/04/c-program-to-find-maximum-and-minimum-in-array-using-recursion.html?
Ralph
I tried the function my teacher gave me, it gave me an answer of 1. I tried to hand calculate it but I don't know why it's 1
Igor🇺🇦
I will check but my code is cpp
So? 🤷‍♂️ This code is a valid C++
Ralph
Yeah i checked it
Ralph
It's not that. I already made the maximum
Igor🇺🇦
I will check but my code is cpp
If you "really" were using proper C++ you'd use std::max.
Igor🇺🇦
Ralph
I sent my code yesterday for the maximum
Ralph
I was using std::max
Ralph
What is peak if not maximum?
I now got a real weird view of it. However i change the vector, the answer is or 1 or -1
Igor🇺🇦
I sent my code yesterday for the maximum
So what is "peak"?
Ralph
I now got a real weird view of it. However i change the vector, the answer is or 1 or -1
I used my teacher's code. He used a for with a return of i and outside of it a return -1
Ralph
So my i is always 1?
Ralph
In my code too, i get rhe same results
Ralph
That's why i am not able to get a definition for peak 😅
Ralph
Nevermind i will ask my doctor tomorrow why is it so. When I add a return -1; at the end like you said guys, i get the same answer as my teacher's
Ralph
So infinitely many thanks guys. I always ask and you always answer 💙
Ralph
@JRandomGuy if you want i can tell you the definition of what peak means here as soon as i know
AHMED
#include<iostream> #include<string> using namespace std; int main() { string day; cout<<"This program will tell you your schedule for the next week.\nEnter a day: \n"; cin>>day; enum myenum{ Saturday, Sunday, Monday, Tuesday, Wednesday, Thurusday, Friday }; switch (myenum){ case "Saturday" : cout<<"off day. Enjoy.\n"; break; case "Sunday" : cout<<"off day. Enjoy.\n"; break; case "Monday" : cout<<"Morning duty at the department.\n"; break; case "Tuesday" : cout<<"24 hours duty.\n"; break; case "Wednesday" : cout<<"off day. Enjoy.\n"; break; case "Thurusday" : cout<<"Morning duty at the OPC.\n"; break; case "Friday" : cout<<"off day. Enjoy.\n"; break; default : cout<<"You entered wrong day.\n"; break; } }
AHMED
The output:
AHMED
<stdin>:18:16: error: expected unqualified-id switch (myenum){ ^ 1 error generated.
AHMED
How to solve this?
H
How to solve this?
eliminate double qoutes case Monday: and you should create a variable of enums ... enum MyEnum{ ... } myEnumVariable;
H
The same output
whole of programm has many problems. learn more.
Phil
did you solve the bug?
yes my malloc wasn't legal I wrote malloc(counter) instead of malloc(counter*sizeof(..))
Pavel
I want the user to enter the day of the week not a number representing the day.
Then you can compare the string input from the user with each string, but switch case can't do that
AHMED
Then you can compare the string input from the user with each string, but switch case can't do that
U mean using multiple if statements . An if statement for each day? 7 if statements and one else statement instead of the default case?
Pavel
U mean using multiple if statements . An if statement for each day? 7 if statements and one else statement instead of the default case?
That would be the most straightforward way. You can also use std::map like std::map<std::string, std::function<void()>>; and fill it with processors of each day.
Pavel
U mean using multiple if statements . An if statement for each day? 7 if statements and one else statement instead of the default case?
Oh, also, looking at your code I see that you can make a map with just <string, string>, and set output string for each of the days of week
AHMED
Oh, also, looking at your code I see that you can make a map with just <string, string>, and set output string for each of the days of week
#include <iostream> #include <string> #include <map> using namespace std; int main() { string day; cout << "This program will tell you your schedule for the next week.\nEnter a day: \n"; cin >> day; map<string, string> mymap = {{"Saturday", "Offday. Enjoy"}, {"Sunday", "Offday. Enjoy"}, {"Monday", "Morning duty at the department"}, {"Tuesday", "24 hours duty."}, {"Wednesday", "Offday. Enjoy."}, {"Thurusday", "Morning duty at the OPC"}, {"Friday", "Offday. Enjoy."}}; cout << "Your schedule for " << day << ":\n"; for (auto el : mymap) { if (day == el.first) { cout << el.second << "\n"; } else { cout << "You entered wrong day.\n"; } } }
AHMED
The output:
AHMED
This program will tell you your schedule for the next week. Enter a day: Monday Your schedule for Monday: You entered wrong day. Morning duty at the department You entered wrong day. You entered wrong day. You entered wrong day. You entered wrong day. You entered wrong day. [Program finished]
AHMED
I typed else for wrong user input
Pavel
I typed else for wrong user input
If you want to make a loop, then you can have a bool variable isFound set it to false in the beginning, then set to true if you find the correct string, then after the loop check the variable and output the error if it is false. But you don't need a loop here, you need to call mymap.find(day)
Pavel
And compare the result with mymap.end() to check if it's correct (should not be equal to end if it's correct), then output second or output your error message
AHMED
And compare the result with mymap.end() to check if it's correct (should not be equal to end if it's correct), then output second or output your error message
#include <iostream> #include <string> #include <map> using namespace std; int main() { string day; cout << "This program will tell you your schedule for the next week.\nEnter a day: \n"; cin >> day; map<string, string> mymap = {{"Saturday", "Offday. Enjoy"}, {"Sunday", "Offday. Enjoy"}, {"Monday", "Morning duty at the department"}, {"Tuesday", "24 hours duty."}, {"Wednesday", "Offday. Enjoy."}, {"Thurusday", "Morning duty at the OPC"}, {"Friday", "Offday. Enjoy."}}; cout << "Your schedule for " << day << ":\n"; auto it = mymap.find(day); if (it != mymap.end()) { cout << it->second; } else { cout << "You entered wrong day.\n"; } }
AHMED
Thanks alot Pavel.
AHMED
Now , if the user entered the day of the week in small letters , the else statement will be executed. How to ignore the size of the letters in string input?
Pavel
Now , if the user entered the day of the week in small letters , the else statement will be executed. How to ignore the size of the letters in string input?
You can write weekday names in lower case and then change the case of the user input to lower before searching. There's a function tolower or something like this to do that.
AHMED
You can write weekday names in lower case and then change the case of the user input to lower before searching. There's a function tolower or something like this to do that.
#include <iostream> #include <string> #include <map> using namespace std; int main() { string day; cout << "This program will tell you your schedule for the next week.\nEnter a day: \n"; cin >> day; transform(day.begin(), day.end(), day.begin(), [](unsigned char a) { return tolower(a); }); map<string, string> mymap = {{"saturday", "Offday. Enjoy"}, {"sunday", "Offday. Enjoy"}, {"monday", "Morning duty at the department"}, {"tuesday", "24 hours duty."}, {"wednesday", "Offday. Enjoy."}, {"thurusday", "Morning duty at the OPC"}, {"friday", "Offday. Enjoy."}}; auto it = mymap.find(day); if (it != mymap.end()) { cout << "Your schedule for " << day << ":\n"; cout << it->second; } else { cout << "You entered wrong day.\n"; } }
AHMED
Now, if the user entered wrong day , I want the program to instruct the user to enter another input as along as he enters a wrong input. HOW to do this?
AHMED
Ok I figured it.
AHMED
#include <iostream> #include <string> #include <map> using namespace std; void myfunction(){ string day; cout << "This program will tell you your schedule for the next week.\nEnter a day: \n"; cin >> day; transform(day.begin(), day.end(), day.begin(), [](unsigned char a) { return tolower(a); }); map<string, string> mymap = {{"saturday", "Offday. Enjoy"}, {"sunday", "Offday. Enjoy"}, {"monday", "Morning duty at the department"}, {"tuesday", "24 hours duty."}, {"wednesday", "Offday. Enjoy."}, {"thurusday", "Morning duty at the OPC"}, {"friday", "Offday. Enjoy."}}; auto it = mymap.find(day); if (it != mymap.end()) { cout << "Your schedule for " << day << ":\n"; cout << it->second; } else { cout << "You entered wrong day.\n"; myfunction(); } } int main() { myfunction(); }
Pavel
Move cin >> and search into the loop and break from it when the input is good
Vladislav
#include <boost/optional.hpp> #include <string> #include <sstream> #include <iostream> enum class StatusCode { OK, ERROR }; struct SResp { std::string m_msg; StatusCode m_status; }; SResp queryToTheMoon(size_t val) { std::stringstream ss; for (size_t idx = 0; idx < val; ++idx) { ss << "[" << val << "]"; } return { ss.str(), StatusCode::OK }; } boost::optional<std::string> makeRequest(size_t val) { auto response = queryToTheMoon(val); if (response.m_status == StatusCode::OK) { return response.m_msg; } else { return boost::none; } } int main() { auto op = makeRequest(100); if (op) { std::cout << op.value() << std::endl; } } Hi C++ Hackers! Maybe, somebody, could help me) It's a task from an interview and they asked me - what is the problem in this code?
1212
Process finished with exit code 134 (interrupted by signal 6: SIGABRT)
1212
opencv 4.5
M
Hello I find a code in C and it helped me to send dns query and receive an answer from it and all things work perfect But when I try ip which is answer of dns server in browser it’s respond is such as server is down Did I do as regular or i have to set something ? Thanks for you helping
Anonymous
Write a c++ Program that solve the following series and display the result. 1/2!+2/3!+3/5!+5/7!+….n/(next prime n)! can anyone know the code
Anonymous
You are welcome and thank you
ࣩࣩࣩࣩࣩࣩࣩࣩࣩࣩࣩࣩࣩࣩࣩࣩࣩࣩࣩࣩࣩࣩࣩࣩࣩࣩࣩࣩࣩࣩࣩࣩࣩࣩࣩࣩࣩࣩࣩࣩࣩࣩࣩࣩࣩࣩࣩࣩࣩࣩࣩࣩࣩࣩࣩࣩࣩࣩࣩࣩࣩࣩࣩࣩ
https://programmatistis.medium.com/
Aakash
#include <boost/optional.hpp> #include <string> #include <sstream> #include <iostream> enum class StatusCode { OK, ERROR }; struct SResp { std::string m_msg; StatusCode m_status; }; SResp queryToTheMoon(size_t val) { std::stringstream ss; for (size_t idx = 0; idx < val; ++idx) { ss << "[" << val << "]"; } return { ss.str(), StatusCode::OK }; } boost::optional<std::string> makeRequest(size_t val) { auto response = queryToTheMoon(val); if (response.m_status == StatusCode::OK) { return response.m_msg; } else { return boost::none; } } int main() { auto op = makeRequest(100); if (op) { std::cout << op.value() << std::endl; } } Hi C++ Hackers! Maybe, somebody, could help me) It's a task from an interview and they asked me - what is the problem in this code?
#include <boost/optional.hpp> #include <string> #include <sstream> #include <iostream> enum struct StatusCode : int { OK=0, ERROR=-1 }; struct SResp { std::string m_msg; StatusCode m_status; }; SResp queryToTheMoon(size_t val) { std::stringstream ss; for (size_t idx = 0; idx < val; ++idx) { ss << "[" << val << "]"; } return { ss.str(), StatusCode::OK }; } boost::optional<std::string> makeRequest(size_t val) { auto response = queryToTheMoon(val); std::cout << "\n r_s: " << typeid(response.m_status).name(); std::cout << "\n s_o: " << typeid(StatusCode::OK).name(); std::cout << "\n m_s: " << typeid(response.m_msg).name(); std::cout << "\n b_n: " << typeid(boost::none).name(); if (response.m_status == StatusCode::OK) { return response.m_msg; //string<char> } else { return boost::none; //exception } } int main() { auto op = makeRequest(5); std::cout << "\n op: " << typeid(op).name(); if (op) { ///??condition & exception: string<char> & boost::none std::cout << "\n Val: " << op.value() << std::endl; } } Unless I’m making a mistake🙃😄