How can I TryParse userinput? - c++-cli

this is my problem:
#include "stdafx.h"
#include <iostream>
#include <string>
#define newline '\n'
using namespace std;
using namespace System;
int _tmain(int argc, _TCHAR* argv[])
{
bool isNumber;
String^ age;
Int32 result;
cout
<< "Please enter your age:"
<< newline;
cin >> age;
isNumber = Int32::TryParse(age, result);
return 0;
}
I cant use cin with a String^ and I can't use string to tryparse

Related

how to static add openssl with c++ builder?

I am able to use openssl in the source code below, but without the libeay32.dll file, my project does not open. I want to add it static and eliminate the need for dlls. How can I do that ?
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include <openssl/bio.h>
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
#pragma comment(lib, "libeay32.lib")
#pragma comment(lib, "ssleay32.lib")
TForm1 *Form1;
void __fastcall TForm1::Button1Click(TObject *Sender)
{
BIO* key;
void* buf;
key=BIO_new_mem_buf(buf,100);
}
//---------------------------------------------------------------------------
I tried to compile statically but without success.

Qt GraphicsScene on Widget

How to add Graphics Scene/Graphics View to Widget?
Code here
This is my Solution:
#include <QApplication>
#include <QWidget>
#include <QVBoxLayout>
#include <QGraphicsView>
#include <QGraphicsTextItem>
#include <QGraphicsScene>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QWidget *window = new QWidget;
window->resize(300, 200);
QVBoxLayout *layout = new QVBoxLayout(window);
QGraphicsScene scene;
QGraphicsView *view = new QGraphicsView(&scene);
QGraphicsTextItem *text = scene.addText("Hello World");
layout->addWidget(view);
window->show();
return a.exec();
}
Output:
Thank you so much for your answer. This also it works.
#include <QApplication>
#include <QWidget>
#include <QVBoxLayout>
#include <QGraphicsView>
#include <QGraphicsTextItem>
#include <QGraphicsScene>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QWidget *window = new QWidget;
window->resize(300, 200);
QVBoxLayout *layout = new QVBoxLayout(window);
QGraphicsScene *scene = new QGraphicsScene(window);
QGraphicsView *view = new QGraphicsView(scene);
QGraphicsTextItem *text = scene->addText("Hello World");
layout->addWidget(view);
window->show();
return a.exec();
}

OSX command line app linker error

I have created an OSX command app in Xcode 5
Here is the main.m
#import <Foundation/Foundation.h>
#import "ConnectionListener.h"
#import "SOMatrix.h"
int main(int argc, const char * argv[])
{
#autoreleasepool {
NSLog(#"Hello, World!");
print_m();
}
return 0;
}
and here is my header file:
#ifndef __GDC1__SOMatrix__
#define __GDC1__SOMatrix__
#ifdef __cplus
#include <iostream>
#endif
int print_m();
#endif /* defined(__GDC1__SOMatrix__) */
And here is a partial listing of the SOMatrix.mm file
#include "SOMatrix.h"
#include <iostream>
using namespace std;
int print_m() {
// logic removed to keep it short; no compile time error
return 0;
}
When I build the project I got a linker error:
Undefined symbols for architecture x86_64:
"_print_m", referenced from:
_main in main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
I don't understand why the function is showhow changed to have a leading underscore in the name ('_print_m').
Why this error occurs? Do I need to add the .mm file explicitly to the project?
You need to change these lines:
#ifdef __cplus
#include <iostream>
#endif
to this in your .h file:
#ifdef __cplusplus
#include <iostream>
extern "C"
{
#endif
with a companion:
#ifdef __cplusplus
}
#endif
at the end of the .h file.
Because you are trying to access a C++ function from Objective-C, and C++ tends to do a bit of name mangling (adding the underscore, for example). Adding the "extern "C"" bit allows your Objective-C code to find your C function declarations. The answers to this related question might elaborate on things a bit better than I can.

Using extern for static variables

I'm seeing something odd when I run some code that uses the extern keyword to reference a static variable within the implementation file. So I declare the static variable gCounter within my implementation file and reference it within two methods in the same implementation file (because its static). However, when i use the extern keyword in my methods i get different results. My understanding (from reading my book) is that extern isn't necessary when you're referencing a static variable declared in the same file as your methods. Code is as follows:
/** interface **/
#import <Foundation/Foundation.h>
#interface Fraction : NSObject
+(Fraction *) allocF;
+(int) count;
#end
/**implementation**/
#import "Fraction.h"
static int gCounter;
#implementation Fraction
+(Fraction *) allocF
{
extern int gCounter;
++gCounter;
return [Fraction alloc];
}
+(int)count
{
extern int gCounter;
return gCounter;
}
#end
/**main**/
#import "Fraction.h"
int main (int argc, const char * argv[])
{
#autoreleasepool
{
Fraction *a, *b, *c;
NSLog(#"The number of fractions allocated: %i", [Fraction count]);
a = [[Fraction allocF] init];
b = [[Fraction allocF] init];
c = [[Fraction allocF] init];
NSLog(#"The number of fractions allocated: %i", [Fraction count]);
}
return(0);
}
When I use the extern keyword in my methods, the code works properly and results in the integer 3 being printed. However, when I remove extern, the integer 2 gets printed. Why is that? Since gCounter is a static variable, shouldn't this work without the extern keyword?
You need to understand the difference between a declaration and a definition:
static int x and int x are definitions. The compiler reserves memory for x.
extern int x on the other hand is a declaration. You tell the compiler that there is a variable x that is defined somewhere else.
Also, you can define different variables in different scopes, that have the same variable name:
int x = 0;
{
int x = 1;
NSLog(#"x = %d", x); // x = 1
}
NSLog(#"x = %d", x); // x = 0
So if you write
int x;
void foo() {
int x;
x++;
}
you are incrementing the function local x.
int x;
void foo() {
x++;
}
increments the global x
int x;
void foo() {
extern int x;
x++;
}
You need to declare extern int x if your definition of x is in another compilation unit, if it's in the same compilation unit, the last two are equivalent.

Bison %code top error

%code top command doesn't include its contents in parser.tab.h file (It should do so, right?). Bison version is 2.4.1. What is the problem with this (simplified) code?
%{
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <io.h>
#define YYDEBUG 0
int errors;
%}
%code top {
struct DICT
{
char *Name;
int Offs;
int Size;
struct DICT *Next;
};
typedef struct DICT DICT;
struct NODE
{
int ID;
int Value;
DICT *Var;
struct NODE *Left;
struct NODE *Right;
};
typedef struct NODE NODE;
}
%{
NODE *Tree = 0;
NODE *Node(int ID, int Value, DICT *Var, NODE *Left, NODE *Right);
void yyerror(char *s)
{
errors++;
printf("%s\n", s);
}
%}
%no_lines
%union
{
int Value;
char *ID;
NODE *Node;
}
EDIT:
with "%code requires" problem was resolved but another arise:
parser.tab.h:40: error: redefinition of 'struct DICT'
parser.tab.h:47: error: redefinition of typedef 'DICT'
parser.tab.c:145: error: previous declaration of 'DICT' was here
Using %code top will not insert the code into the header but only into the source file. It is well documented here.
I guess %code provides (or %code requires) will be more suited because it inserts the definitions in both source and header file.