GetOpt not working on Mac, none of the arguments show up - c++

int c = 0;
while (c = getopt(argc, argv, "p:t:e:") != -1) {
std::cout<<"c: "<<c<<std::endl;
switch (c) {
case 'p':
if (optarg) {
std::cout << "lol" << std::endl;
person = atoi(optarg);
}
break;
case 't':
if (optarg) {
time = stod(optarg);
std::cout << "ll" << std::endl;
}
break;
case 'e':
if (optarg) {
ecg = atoi(optarg);
std::cout << "2dasf" << std::endl;
}
break;
}
}
Been trying to make getopt work on my Mac. C is printed as 1, but None of the print statements within the switch are printed. This works perfectly in Linux. What is wrong with my Mac?

Related

Query about a given function's use in the code

i'm following this tutorial and they give us this code to test the function isLowerVowel:
#include <iostream>
bool isLowerVowel(char c, bool yIsVowel)
{
switch (c)
{
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
return true;
case 'y':
return (yIsVowel ? true : false);
default:
return false;
}
}
int main()
{
std::cout << std::boolalpha;
std::cout << isLowerVowel('a',false) << "\n";
std::cout << isLowerVowel('a',true) << "\n";
std::cout << isLowerVowel('q',false) << "\n";
std::cout << isLowerVowel('q',true) << "\n";
std::cout << isLowerVowel('y',false) << "\n";
std::cout << isLowerVowel('y',true) << "\n";
return 0;
}
I dont understand what the use of yIsVowel is for, shouldnt just testing isLowerVowel be enough? Sorry i asked them but got no replies
I dont understand what the use of yIsVowel is for, shouldnt just testing isLowerVowel be enough?
If you were to use the isLowerVowel fuction to implement the isLowerVowel function you would have recursion. It is unclear how this recursion should be terminated.
yIsVowel appears to be used to set whether y is a vowel or not.

writing a program which helps the tudio to convert midi note number to name and octave

I want to write a C++ program that converts MIDI note number to name and octave using the switch statements ?
I am having a few errors in my code the code should print out a Note name and octave number (eg. input -> 24 ; output -> C1).
https://www.inspiredacoustics.com/en/MIDI_note_numbers_and_center_frequencies
int main() {
int mi;
cout <<"MIDI value: " << std::endl;
cin >> mi;
int oct_v;
oct_v = (mi/12) - 1;
string notes;
notes = "C C# D D# E F F# G G# A A# B";
string nt;
nt = notes.substr((mi % 12) * 2 , (mi % 12) * 2 +2);
cout <<"Note name " << nt << oct_v;
return 0;
}
The code is not perfect and I am having doubts about what should I put into switch statements.
First some hints:
"I want to write a C++ script"
The term script is typically used for interpreted languages like shell scripts. As c++ is a compiled language we don't name them script. It is a c++ program or c++ source code.
" what should I put into switch statements."
You don't use any switch statement! You pick some substring from a string. That has nothing to do with a switch statement!
What is wrong with your code:
Your calculation for picking the sub string is quite fine, but you assume that your notes inside your string are always have a length of 2 but you use 2 chars for e.g. "C " or 3 chars for "A# ". You simply should write your string with 2 letters per note as:
std::string notes = "C C#D D#E F F#G G#A A#B";
The second parameter of substr is the length and not the end position.
In addition: You should not use using namespace std;
Here are some alternative solutions also with switch statement.
int main() {
int mi;
std::cout <<"MIDI value: " << std::endl;
std::cin >> mi;
unsigned int oct_v = mi/12 -1;
// variant with array
std::string notes[]={"C","C#","D","D#","E","F","F#","G","G#","A","A#","B"};
std::string nt = notes[mi%12];
// variant with substring ( from your code, but fixed )
std::string notes2= "C C#D D#E F F#G G#A A#B";
std::string nt2 = notes2.substr((mi % 12) * 2 , 2);
std::string nt3;
// with switch
switch ( mi%12 )
{
case 0: nt3 = "C"; break;
case 1: nt3 = "C#"; break;
case 2: nt3 = "D"; break;
case 3: nt3 = "D#"; break;
case 4: nt3 = "E"; break;
case 5: nt3 = "F"; break;
case 6: nt3 = "F#"; break;
case 7: nt3 = "G"; break;
case 8: nt3 = "G#"; break;
case 9: nt3 = "A"; break;
case 10: nt3 = "A#"; break;
case 11: nt3 = "B"; break;
}
std::cout <<"Note name " << nt << ":" << oct_v << std::endl;
std::cout <<"Note name2 " << nt2 << ":" << oct_v << std::endl;
std::cout <<"Note name3 " << nt3 << ":" << oct_v << std::endl;
return 0;
}
I think you'd better use a table of char arrays to store the notes. I propose the following code :
#include <iostream>
#include <string>
const char notes[12][4]={"C","C#","D","D#","E","F","F#","G","G#","A","A#","B"};
void midiToNote(int midi, int& octave,std::string& note)
{
octave = ( (midi-12)/12 );
note = std::string(notes[midi%12]);
}
int main()
{
int midi;
int oct;
std::string name;
midi = 29;
midiToNote(midi,oct,name);
std::cout << midi <<" is " <<name << oct<< std::endl;
midi = 51;
midiToNote(midi,oct,name);
std::cout << midi <<" is "<< name << oct << std::endl;
return 0;
}
Hope this helps.

fast string parser C++

I have an idea to make super fast command parser.
I have more than 100 pairs of command - function, and some commands have same prefixes.
Down below there is example of my idea. I can make a program that will generate C++ code like in this example, but i think this can be realized with templates.
I'm not strong in templates. May be some one can help with it?
static const string_view s1{"hello"};
void f1() { cout << "f1" << endl; }
static const string_view s2{"helly"};
void f2() { cout << "f2" << endl; }
static const string_view s3{"hi jo"};
void f3() { cout << "f3" << endl; }
static const string_view s4{"hoyMo"};
void f4() { cout << "f4" << endl; }
void sw(string_view& hw){
switch(hw.size()){
case 5: {
switch(hw[0]){
case 'h': {
switch(hw[1]){
case 'e': {
switch(hw[2]){
case 'l': {
switch(hw[3]){
case 'l': {
switch(hw[4]){
case 'o': {
f1();
break;
}
case 'y': {
f2();
break;
}
default: cout << "command not found" << endl; break;
}
break;
}
default: cout << "command not found" << endl; break;
}
break;
}
default: cout << "command not found" << endl; break;
}
break;
}
case 'i': {
if(hw.substr(2) == s3.substr(2)){
f3();
}
break;
}
case 'o': {
if(hw.substr(2) == s4.substr(2)){
f4();
}
break;
}
default: cout << "command not found" << endl; break;
}
break;
}
default: cout << "command not found" << endl; break;
}
break;
}
case 6: {
//...
break;
}
default: cout << "command not found" << endl; break;
}
}
int main(){
string_view myCommand("hi jo");
sw(myCommand);
string_view myCommand2("hoyMo");
sw(myCommand2);
string_view myCommand3("ha ha");
sw(myCommand3);
}
You should probably be using a parser library, such as Boost.Spirit. This wil allow you to write simple code, like
string("hello")
| string("helly")
| string("hi jo")
| string("hoyMo")
and do all the heavy lifting for you to generate a parser that will probably be faster than something you would write yourself.

atof coredump with getopt

I'm writing a C++ application which converts fahrenheit to celsius and kelvin, and kelvin to celsius and fahrenheit, etc. Since it's stupid to write an interative application here, I decided to familiarize myself with the getopt function in unistd.h.
Format:
F2C -k 273.15
Output:
FAHR CELSIUS KELVIN
32 0 273.15
Here is my code:
#include <iostream>
#include <stdlib.h>
#include <unistd.h>
#define VERSION 0.1
#define HELP help(argv[0])
#define OPTS "vk:f:c:h"
float ver = (float)VERSION;
void help(char *s);
namespace Fahrenheit
{
float FK(float F) {
return ((5.0/9.0) * (F - 32.0) + 273.15);
}
float FC(float F) {
return ((5.0/9.0) * (F - 32.0));
}
void printfahr(float F) {
std::cout << "FAHR\t\tCELSIUS\t\tKELVIN" << std::endl;
std::cout << F << "\t\t" << FC(F) << "\t\t" << FK(F) << std::endl;
}
}
namespace Celsius
{
float CF(float C) {
return ((C*(9/5)) + 32);
}
float CK(float C) {
return (C+273.15);
}
void printc(float C) {
std::cout << "FAHR\t\tCELSIUS\t\tKELVIN" << std::endl;
std::cout << CF(C) << "\t\t" << C << "\t\t" << CK(C) << std::endl;
}
}
namespace Kelvin
{
float KF(float K) {
return (((9.0/5.0) * (K-273.15)) + 32);
}
float KC(float K) {
return (K-273.15);
}
void printk(float K) {
std::cout << "FAHR\t\tCELSIUS\t\tKELVIN" << std::endl;
std::cout << KF(K) << "\t\t" << KC(K) << "\t\t" << K << std::endl;
}
}
int main(int argc, char *argv[])
{
char arg = '\0';
if(argc < 2 && argc == 1 && argc > 0) {
help(argv[0]);
exit(1);
}
/*** Use function getopt() defined in unistd.h to accept 5 arguments: -v, -h, -k, -f, and -c ***/
while((arg=getopt(argc, argv, OPTS))!=-1)
{
float floatarg = atof(optarg);
switch(arg)
{
case 'v':
std::cout << "The current version is:" << ver << std::endl;
break;
case 'h':
HELP;
break;
case 'k':
Kelvin::printk(floatarg);
break;
case 'f':
Fahrenheit::printfahr(floatarg);
break;
case 'c':
Celsius::printc(floatarg);
break;
default:
HELP;
break;
}
}
return 0;
}
void help(char *s) {
std::cout << "Usage:\t"<< s << " [-option] [argument]" << std::endl;
std::cout << "option:\t" << "-c [temperature]: convert a Celsius temperature to Fahrenheit and Kelvin" << std::endl;
std::cout << "\t" << "-f [temperature]: convert a Fahrenheit temperature to Celsius and Kelvin" << std::endl;
std::cout << "\t" << "-h: show help information" << std::endl;
std::cout << "\t" << "-k [temperature]: convert a Kelvin temperature to Fahrenheit and Celsius" << std::endl;
std::cout << "\t" << "-v: show version information" << std::endl;
}
My problem is that whenever I use an option that accepts no arguments (like -v) I get a core dump.
dbx has shown me that the SIGSEV occurs at line 70 (float floatarg = atof(optarg);).
When I run the program like this:
./F2C -k 273.15
The math is done correctly and I get a clear printout. It's only when I use -v or -h that my program SIGSEV's.
Extra information:
This program was compiled with the Sun studio compiler suite, version 5.12.
I'm completely baffled as to why my program SIGSEV's. It is inconsistent and makes no sense.
I would appreciate any help available.
Should have done some optarg checking. After all, you can't convert null to a float.
new main():
#define FLOATARG atof(optarg)
int main(int argc, char *argv[])
{
char arg = '\0';
if(argc < 2 && argc == 1 && argc > 0) {
help(argv[0]);
exit(1);
}
/*** Use function getopt() defined in unistd.h to accept 5 arguments: -v, -h, -k, -f, and -c ***/
while((arg=getopt(argc, argv, OPTS))!=-1)
{
switch(arg)
{
case 'v':
std::cout << "The current version is: << ver << std::endl;
break;
case 'h':
HELP;
break;
case 'k':
Kelvin::printk(FLOATARG);
break;
case 'f':
Fahrenheit::printfahr(FLOATARG);
break;
case 'c':
Celsius::printc(FLOATARG);
break;
default:
HELP;
break;
}
}
return 0;
}
The shortest fix is:
float floatarg = optarg ? atof(optarg) : 0.0;
You can also rewrite your code like
float floatarg = 0.0;
switch(arg)
{
case 'v':
std::cout << "The current version is:" << ver << std::endl;
break;
case 'h':
HELP;
break;
case 'k':
floatarg = atof(optarg);
Kelvin::printk(floatarg);
break;
case 'f':
floatarg = atof(optarg);
Fahrenheit::printfahr(floatarg);
break;
...
or
float floatarg = 0.0;
if(optarg) {
floatarg = atof(optarg);
}
switch(arg)
{
case 'v':
std::cout << "The current version is:" << ver << std::endl;
break;
case 'h':
HELP;
break;
case 'k':
Kelvin::printk(floatarg);
break;
case 'f':
Fahrenheit::printfahr(floatarg);
break;
...

How to perform same action as changing Windows 7 display from Landscape to Portrait

I need to write a program which programmatically changes the Landscape mode to Portrait of a touchscreen in Windows 7. The resolution is 1920 x 1080 and I thought I could switch the 1920 with the 1080 and use ChangeDisplaySettings or ChangeDisplaySettingsEx to make the change. but it doesn't work.
Windows CE has some sort of rotate feature, so I tried that. as in:
devmode.dmFields = DM_DISPLAYORIENTATION;
devmode.dmDisplayOrientation = DMDO_90;
ChangeDisplaySettingsEx(NULL,&devmode,NULL,CDS_RESET,NULL);
But that didn't work on a normal test desktop PC.
I can do this through Control Panel, Display, Screen Resolution, choose Portrait, so should I not be able to do via Windows API?
How can I make this change in code?
EDIT
I tried this code, but I get DISP_CHANGE_BADMODE returned by ChangeDisplaySettings.
#include <Windows.h>
#include <iostream>
int main() {
DEVMODE dm;
// initialize the DEVMODE structure
ZeroMemory(&dm, sizeof(dm));
dm.dmSize = sizeof(dm);
if (0 != EnumDisplaySettings(NULL, ENUM_CURRENT_SETTINGS, &dm))
{
std::cout << "Orientation\n";
switch(dm.dmDisplayOrientation) {
case DMDO_DEFAULT: std::cout << "DMDO_DEFAULT\n"; break;
case DMDO_90: std::cout << "DMDO_90\n"; break;
case DMDO_180: std::cout << "DMDO_180\n"; break;
case DMDO_270: std::cout << "DMDO_270\n"; break;
}
std::cout << "Panning height: " << dm.dmPanningHeight << '\n'
<< "Panning width: " << dm.dmPanningWidth << '\n';
std::cout << "Colour resolution: " << dm.dmBitsPerPel << " bits per pixel\n";
std::cout << "Height in pixels: " << dm.dmPelsHeight << '\n';
std::cout << "Width in pixels: " << dm.dmPelsWidth << '\n';
if(dm.dmPelsHeight > dm.dmPelsWidth) {
std::cout << "Screen should be rotated 90 degrees\n";
std::cout << "Do you want to rotate display 90 degrees? (y or n)\n";
char ch;
std::cin >> ch;
if(ch == 'y') {
// swap height and width
DWORD tmp = dm.dmPelsHeight;
dm.dmPelsHeight = dm.dmPelsWidth;
dm.dmPelsWidth = tmp;
dm.dmFields = DM_PELSWIDTH | DM_PELSHEIGHT;
// determine new orientation
switch (dm.dmDisplayOrientation)
{
case DMDO_DEFAULT:
dm.dmDisplayOrientation = DMDO_270;
break;
case DMDO_270:
dm.dmDisplayOrientation = DMDO_180;
break;
case DMDO_180:
dm.dmDisplayOrientation = DMDO_90;
break;
case DMDO_90:
dm.dmDisplayOrientation = DMDO_DEFAULT;
break;
default:
// unknown orientation value
// add exception handling here
break;
}
LONG ret = ChangeDisplaySettings(&dm, CDS_TEST); //0);
std::cout << "ChangeDisplaySettings returned " << ret << '\n';
switch(ret) {
case DISP_CHANGE_SUCCESSFUL: std::cout << "display successfully changed\n"; break;
case DISP_CHANGE_BADDUALVIEW: std::cout << "The settings change was unsuccessful because the system is DualView capable\n"; break;
case DISP_CHANGE_BADFLAGS: std::cout << "An invalid set of flags was passed in.\n"; break;
case DISP_CHANGE_BADMODE: std::cout << "The graphics mode is not supported.\n"; break;
case DISP_CHANGE_BADPARAM: std::cout << "An invalid parameter was passed in. This can include an invalid flag or combination of flags.\n"; break;
case DISP_CHANGE_FAILED: std::cout << "The display driver failed the specified graphics mode.\n"; break;
case DISP_CHANGE_NOTUPDATED: std::cout << "Unable to write settings to the registry.\n"; break;
case DISP_CHANGE_RESTART: std::cout << "The computer must be restarted for the graphics mode to work.\n"; break;
}
}
}
else
std::cout << "Screen orientation is just fine\n";
}
}
I saw an article on the web indicating that possibly it couldn't be done. But this code works.
#include <Windows.h>
#include <iostream>
int main(int argc, char* argv[]) {
DEVMODE dm;
// initialize the DEVMODE structure
ZeroMemory(&dm, sizeof(dm));
dm.dmSize = sizeof(dm);
// only change first/default display (index=0)
if (0 != EnumDisplaySettings(NULL, ENUM_CURRENT_SETTINGS, &dm))
{
std::cout << "Current orientation\n";
switch(dm.dmDisplayOrientation) {
case DMDO_DEFAULT: std::cout << "DMDO_DEFAULT\n"; break;
case DMDO_90: std::cout << "DMDO_90\n"; break;
case DMDO_180: std::cout << "DMDO_180\n"; break;
case DMDO_270: std::cout << "DMDO_270\n"; break;
}
std::cout << "Panning height: " << dm.dmPanningHeight << '\n'
<< "Panning width: " << dm.dmPanningWidth << '\n';
std::cout << "Colour resolution: " << dm.dmBitsPerPel << " bits per pixel\n";
std::cout << "Height in pixels: " << dm.dmPelsHeight << '\n';
std::cout << "Width in pixels: " << dm.dmPelsWidth << '\n';
if(argc != 2) {
std::cout << "Usage: rotate_screen <angle>. Angle values: 0, 90, 180, 270. Angles are rotation clockwise. Use 0 to revert back\n";
exit(0);
}
// parse parameter
int rotate = atoi(argv[1]); // should do something better and safer here
if(rotate != 0 && rotate != 90 && rotate != 180 && rotate != 270) {
std::cout << "incorrect rotation selected\n";
}
else {
// swap height and width
DWORD tmp = dm.dmPelsHeight;
dm.dmPelsHeight = dm.dmPelsWidth;
dm.dmPelsWidth = tmp;
// select fields which have changed
dm.dmFields = DM_PELSWIDTH | DM_PELSHEIGHT | DM_DISPLAYORIENTATION;
// set angle
switch(rotate) {
case 0: dm.dmDisplayOrientation = DMDO_DEFAULT; break;
case 90: dm.dmDisplayOrientation = DMDO_90; break;
case 180: dm.dmDisplayOrientation = DMDO_180; break;
case 270: dm.dmDisplayOrientation = DMDO_270; break;
default:
std::cout << "Something went wrong, aborting\n";
exit(0);
}
LONG ret = ChangeDisplaySettingsEx(NULL, &dm, NULL, 0, NULL); //CDS_RESET, NULL); //0);
std::cout << "ChangeDisplaySettingsEx returned " << ret << '\n';
switch(ret) {
case DISP_CHANGE_SUCCESSFUL: std::cout << "display successfully changed\n"; break;
case DISP_CHANGE_BADDUALVIEW:
std::cout << "The settings change was unsuccessful because the system is DualView capable\n";
break;
case DISP_CHANGE_BADFLAGS: std::cout << "An invalid set of flags was passed in.\n"; break;
case DISP_CHANGE_BADMODE: std::cout << "The graphics mode is not supported.\n"; break;
case DISP_CHANGE_BADPARAM: std::cout << "An invalid parameter was passed in. This can include an invalid flag or combination of flags.\n"; break;
case DISP_CHANGE_FAILED: std::cout << "The display driver failed the specified graphics mode.\n"; break;
case DISP_CHANGE_NOTUPDATED: std::cout << "Unable to write settings to the registry.\n"; break;
case DISP_CHANGE_RESTART: std::cout << "The computer must be restarted for the graphics mode to work.\n"; break;
}
}
} // enum worked
}