Getting Error is cs0162 unreachable code detected - console-application

using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
}
decimal ParseDecimal(string number)
{
if (number.Equals("0E+3", StringComparison.OrdinalIgnoreCase))
{
return 0;
}
return decimal.Parse(number, System.Globalization.NumberStyles.Any);
Console.WriteLine(number);
}
}
}

This line is unreachable:
Console.WriteLine(number);
And that is because you are returning from the function in the preceding line.

Related

How can I use variables in C++ without passing into a function

Is there a way in C where you can use variables that you don't pass in variables?
I'm working on some code that someone else started on and I'm just modifying it.
Here is some code that I have.
#include <iostream>
using namespace std;
void rotate90() {
for (int w=0;w<n;w++) {
for (int h=0;h<n;h++) {
chart2[h][w]=chart[n1-w][n1-h];
}
}
for (int h=0;h<n;h++) {
for (int w=0;w<n;w++) {
chart[h][w]=chart2[h][w];
}
}
}
int main() {
......
else
{
rotate90;
}
}
}

error: expected type-specifier before 'Mobile'

I did a research but solution is not matching to my program. From the large code i took only erroneous part. Please suggest.
#include <iostream>
using namespace std;
class Products {
static Products* prod;
public:
//Creat a static funtion which does the task what client was doing
static void checkStockStatus(int choice)
{
switch (choice) {
case 1:
prod = new Mobile();
break;
}
prod->inStockStatus();
}
virtual void inStockStatus() = 0;
};
class Mobile : public Products {
public:
void inStockStatus()
{
cout << "Mobiles are instock" << endl;
}
};
int main()
{
int choice = 1;
Products::checkStockStatus(choice);
return 0;
}
OutPut:
In static member function 'static void Products::checkStockStatus(int)':
14:29: error: expected type-specifier before 'Mobile'

getting headers value in proxygen

i am using the proxygen for making a simple web server. i am restricted to use proxygen.I am using proxygen default echo server example i want to print the header values whenever a request is sent to the server. below is the code that i think i should modify.but what exactly i am unsure of.:
#include "customHandler.h"
#include <iostream>
#include <proxygen/httpserver/RequestHandler.h>
#include <proxygen/httpserver/ResponseBuilder.h>
#include <proxygen/lib/http/HTTPMessage.h>
#include <proxygen/lib/http/HTTPMethod.h>
using namespace std;
using namespace proxygen;
namespace EchoService {
EchoHandler::EchoHandler(EchoStats* stats): stats_(stats) {
}
void EchoHandler::onRequest(std::unique_ptr<HTTPMessage> headers) noexcept {
//------------------HERE TO MODIFY I THINK-------------------//
}
void EchoHandler::onBody(std::unique_ptr<folly::IOBuf> body) noexcept {
if (body_) {
body_->prependChain(std::move(body));
} else {
body_ = std::move(body);
}
}
/*
.header("Request-Number",
//this sets the request number
folly::to<std::string>(stats_->getRequestCount()),"test-b")*/
void EchoHandler::onEOM() noexcept {
ResponseBuilder(downstream_)
.status(200, "OK")
//Response is set Here...........ex-> .body(std::move("some Response object"))
.body(std::move(body_))
.sendWithEOM();
}
void EchoHandler::onUpgrade(UpgradeProtocol protocol) noexcept {
// handler doesn't support upgrades
}
void EchoHandler::requestComplete() noexcept {
delete this;
}
void EchoHandler::onError(ProxygenError err) noexcept {
delete this;
}
}
correct me if i am wrong.
Try this:
void EchoHandler::onRequest(std::unique_ptr<HTTPMessage> headers) noexcept
{
HTTPHeaders head = headers->getHeaders();
head.forEach([&] (const string& header, const string& val) {
cout << header << ": " << val<<endl;
});
}

Try/catch crash 255

new to the try/catch thing and need to use it for a program. For some reason though my program crashes when it catches the obj. The sample program I wrote just reads in a number and then tests to see if its below 10 or above 20. The point is to use two user defined classes to throw and catch. The above 20 obj gets thrown and catches just fine. But the below 10, if its thrown will crash the program. Why is this? here is my code
#include <iostream>
#include <cstdlib>
using namespace std;
class above20
{
public:
above20()
{
msg = "Number is above 20.";
};
string getmsg()
{
cout<<msg<<endl;
};
private:
string msg;
};
class below10
{
public:
below10()
{
msg = "Number is below 10";
};
string getmsg()
{
cout<<msg<<endl;
};
private:
string msg;
};
int main ()
{
int num;
try
{
cout<<"Enter Number between 10 and 20:"<<endl;
cin >> num;
if (num > 20)
{
throw above20 ();
}
}
catch (above20 obj)
{
obj.getmsg();
}
try
{
if (num < 10)
{
throw below10 ();
}
}
catch (below10 obj)
{
obj.getmsg();
}
system ("pause");
return (0);
}
Are you sure this compiles? Did you omit something in your copy paste? The getmsg() methods don't return anything.
---edit ---
Try this:
void getmsg()
{
cout<<msg<<endl;
};
You have a lot of poor syntax in your code. The error you get is because you declare getMsg with a return value and do not return (UB - you are lucky it even compiles!).
To fix all of your issues: http://ideone.com/1qGAuR
You have a few errors in your code like having a function that doesn’t return anything despite saying that it should in the signature:
string getmsg()
{
cout<<msg<<endl;
};
should really be:
void getmsg()
{
cout<<msg<<endl;
};
or
string getmsg()
{
return string(msg);
};
Putting aside those bugs for a second, from a design point of view inheriting from one exception base class is cleaner. I would usually inherit from std::runtime_error but you could define your own if you wanted. For example :
#include <iostream>
#include <cstdlib>
using namespace std;
class above_below_exception
{
public:
virtual string getmsg() =0;
};
class above20 : public above_below_exception
{
public:
above20()
{
msg = "Number is above 20.";
};
string getmsg()
{
return string(msg);
};
private:
string msg;
};
class below10 : public above_below_exception
{
public:
below10()
{
msg = "Number is below 10";
};
string getmsg()
{
return string(msg);
};
private:
string msg;
};
int main ()
{
int num;
try
{
cout<<"Enter Number between 10 and 20:"<<endl;
cin >> num;
if (num > 20)
{
throw above20();
}
if (num < 10)
{
throw below10();
}
}
catch (above_below_exception& obj)
{
cout << obj.getmsg();
}
return (0);
}
A potential fix for your code:
#include <iostream>
#include <cstdlib>
using namespace std;
class above20 : public std::exception
{
public:
above20()
{
msg = "Number is above 20.";
}
virtual ~above20 () throw ()
{
}
string getmsg ()
{
cout << msg << endl;
return msg;
}
private:
string msg;
};
class below10 : public std::exception
{
public:
below10()
{
msg = "Number is below 10";
}
virtual ~below10 () throw ()
{
}
string getmsg()
{
cout << msg << endl;
return msg;
}
private:
string msg;
};
int main ()
{
int num;
try
{
cout<<"Enter Number between 10 and 20:"<<endl;
cin >> num;
if (num > 20)
{
throw above20 ();
}
}
catch (above20 &obj)
{
cout << obj. getmsg () << endl;
}
try
{
if (num < 10)
{
throw below10 ();
}
}
catch (below10 obj)
{
obj.getmsg();
}
system ("pause");
return (0);
}

How to correctly handle errors when using boost::filesystem?

first, here is some code:
class A
{
public:
A()
{
//...
readTheFile(mySpecialPath);
//...
}
A(boost::filesystem::path path)
{
//...
readTheFile(path);
//...
}
protected:
void readTheFile(boost::filesystem::path path)
{
//First, check whether path exists e.g. by
//using boost::filesystem::exists(path).
//But how to propagate an error to the main function?
}
//...
};
int main(int argc, char **argv)
{
A myClass;
//Some more code which should not be run when A::readTheFile fails
}
What is a good solution to let the main function know that A::readTheFile could not open the file? I want to terminate the execution when opening the file fails.
Many thanks in advance!
Have readTheFile() throw an exception:
protected:
void readTheFile(boost::filesystem::path path)
{
//First, check whether path exists e.g. by
//using boost::filesystem::exists(path).
//But how to propagate an error to the main function?
if (/*some-failure-occurred*/)
{
throw std::runtime_error("Failed to read file: " + path);
}
}
...
int main()
{
try
{
A myObj;
//Some more code which should not be run when A::readTheFile fails
}
catch (const std::runtime_error& e)
{
std::cerr << e.what() << "\n";
}
return 0;
}