How to display global variable on messagebox? - c++-cli

This is written code,
void horizontal_calculate()
{
String ^aa = filenames[0];
std::string file1(marshal_as<std::string>(aa));
String ^bb = filenames[1];
std::string file2(marshal_as<std::string>(bb));
double Result3=horizontal_read(file1);
double Result4=horizontal_read(file2);
double result=Result3/Result4;
result1=result;
System::Diagnostics::Debug::WriteLine("{0}",result);
}
private: System::Void button4_Click(System::Object^ sender, System::EventArgs^ e) {
for (int i = 0; i < filenames->Length; i++)
System::Diagnostics::Debug::WriteLine(filenames[i]);
semicircle();
horizontal_calculate();
oblique();
MessageBox::Show("Time Ratio = "result1"","Screening Result",MessageBoxButtons::OK, MessageBoxIcon::Information);
}
I have declared double=result1 as global variable.
It comes out an error "error C2146: syntax error : missing ')' before identifier 'result1'", so how am I going to solve this?
is it needed and how's the way to convert double to string?
Thanks all.

If the problem is in MessageBox line, write it by the following way:
MessageBox::Show(
String::Format("Time Ratio = {0}", result1),
"Screening Result",
MessageBoxButtons::OK, MessageBoxIcon::Information);

Related

Why can't labels prints integrers?

private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
int result = 0;
result += 1;
label1->Text = result;
}
Can someone tell me how to fix this code?
value += 1;
String^ lol = System::Convert::ToString(value);
label1->Text = lol;

Inconsistent method declarations in duplicated types

I'm making a class in c++-cli, and I added an extra argument to one of my functions. The name of the extra argument was int row,and when I add that I get this error:
LNK2022 metadata operation failed (80131187) : Inconsistent method declarations in duplicated types (types: query; methods: Read_DB)
When I remove that extra argument I added, the error goes away. If I remove the argument column_index, it also goes away. But if I remove one of the String^ arguments, it still stays. I'm not sure what the error is, here's both my .h and the functions definition in the .cpp file of code:
Header:
#ifndef DATA_BASE
#define DATA_BASE
using namespace System;
using namespace System::Data::SqlClient;
ref class ConnectDB{
protected:
SqlConnection^ cnn;
bool state;
public:
String^ db;
bool ConnectDataBase();
bool DisconnectDataBase(void);
};
ref class Query : public ConnectDB {
private:
~Query(void);
public:
bool Create_Table(String^ name, String^ columns);
String^ Read_DB(String^ column, String^ table, int column_index, int row);
bool Write_DB(String^ path, String^ msg);
};
#endif
cpp file:
String^ Query::Read_DB(String^ column, String^ table, int column_index, int row) {
String^ output;
String^ sql = "SELECT " + column + " FROM " + table;
try {
SqlCommand^ command;
SqlDataReader^ dataReader;
command = gcnew SqlCommand(sql, cnn);
dataReader = command->ExecuteReader();
std::cout << "Reading data from Database...\n";
int counter;
while (dataReader->Read()) {
counter++;
if(counter == row)
output = (String^)dataReader->GetValue(column_index);
}
//command->Dispose();
dataReader->Close();
}
catch (Exception^ e) {
Console::WriteLine(e);
std::cout << "Failed to query database\n";
return "0";
}
return output;
}
to fix the problem I just deleted the debug folder and restarted my application. It had something to do with changing the name of the object.

What do the various portions of a compiler returned error mean?

The question is pretty basic.
Code:
#include<iostream>
#include<fstream>
using namespace std;
struct Customer
{
char name[20];
char cell[12];
double bal;
};
int get_total_records_from_file(char * filename)
{
// Implement this funtion
return 0;
}
void get_input_from_user(Customer * ptr){
cin.ignore();
for(int i = 0; i < customer_count; i++){
cout<<"Enter Customer Name (1-20 character long)"<<endl;
cin.getline(ptr[i].name,20);
cout<<"Enter Cell No. (11 character long)"<<endl;
cin.getline(ptr[i].cell,20);
cout<<"Enter Initial Balance"<<endl;
cin >> ptr[i].bal;
cin.ignore();
}
}
If I have this error:
main.cpp:22:21: error: ‘customer_count’ was not declared in this scope
what do the various portions of it mean? That is, what's 22? what's 21? etcetra.

How to read next line in csv file on button click

I have a windows form with two butttons and a text box. My start button reads the first line in the csv file and outputs the data I want into a textbox:
private: System::Void StartBtn_Click(System::Object^ sender, System::EventArgs^ e)
{
String^ fileName = "same_para_diff_uprn1.csv";
StreamReader^ din = File::OpenText(fileName);
String^ delimStr = ",";
array<Char>^ delimiter = delimStr->ToCharArray( );
array<String^>^ words;
String^ str = din->ReadLine();
words = str->Split( delimiter );
textBox1->Text += gcnew String (words[10]);
textBox1->Text += gcnew String ("\r\n");
textBox1->Text += gcnew String (words[11]);
textBox1->Text += gcnew String ("\r\n");
textBox1->Text += gcnew String (words[12]);
textBox1->Text += gcnew String ("\r\n");
textBox1->Text += gcnew String (words[13]);
Then my 'next button' I want it to clear the text box, and display the next lines data as above. Then everytime the next button is cliced, the textbox is cleared and the next line of the csv file is shown. Until I get to the end of the file. How would I manage that?
TIA
Your problem is that your button_click() function forgets the StreamReader object and all other variables after it has finished.
You need to make some of the variables (at least din) independent from the function, defining them as members of your WinForms object. Whenever you call the function, you can read the next line then. And you need to add a check whether din is nullptr (will be so at the first call), then load the file, otherwise just use it:
StreamReader^ din;
private: System::Void StartBtn_Click(System::Object^ sender, System::EventArgs^ e)
{
String^ fileName = "same_para_diff_uprn1.csv";
if (!din) // or: if (din == nullptr)
din = File::OpenText(fileName);
String^ delimStr = ",";
...

Why can't I get input from the ifstream?

I am trying to read in a text file for a maze program. The input is something like:
10 10
OO+E+OO+++
O++O+O+OOO
OOOOOO+O+O
+++++O++OO
OOO+OOO+O+
O+O+O+++O+
O+O+OOO+OO
++O+++O++O
O+OOOOO++O
O+O++O+OOO
When the user click on the open button, this opens a open file dialog box
{
openFileDialog1->InitialDirectory = "C:\Desktop;";
openFileDialog1->Filter = "Maze files (*.DAT)|*.DAT";
if (openFileDialog1->ShowDialog() == ::DialogResult::OK)
{
char filename[1024];
for (int i = 0; i < openFileDialog1->FileName->Length; i++)
{
filename[i] = openFileDialog1->FileName[i];
}
ifstream ifs;
ifs.open(filename); // NULL terminate this
maze = new Maze( panel1, ifs);
ifs.close();
}
}
the following is the maze constructor
Maze::Maze( Panel ^ drawingPanel, ifstream & ifs )
{
try
{
valid = false;
ifs >> width >> height;
int temp = width;
drawingPanel->Size.Width = width;
drawingPanel->Size.Height = height;
for (int i = 0; i < height; i++) // height is always nothing
for (int j = 0; j < width; j++)
{
if (orig[j][i] == DEADEND ||
orig[j][i] == OPEN ||
orig[j][i] == EXIT )
ifs >> orig[j][i]; // NULLS????
else
throw 'D'; // i had to throw something....so i threw the D /* make a slit class and throw the D there? slit.fill(D); */
}
// this should be last
panel = drawingPanel;
valid = true;
}
catch (...)
{
valid = false;
MessageBox::Show( "Not a proper maze file!" );
}
}
when the program runs: ifs >> width >> height width and height do not get set correctly.
I have searched this site for this problem and have not been able to find anything that has helped. Sorry for my inexperience, any help is greatly appreciated.
You'e program very ugly : don't know if you're programming in C or C++ or C++/CLI, or try to mix the 3...
Because you use Windows Form projet, i will give you a .Net solution for read a file, it's not the better solution but this does not mix things.
First for read the file, on a first window :
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e)
{
openFileDialog1->Filter = "Maze Files (*.dat) | *.dat";
if (openFileDialog1->ShowDialog() == ::DialogResult::OK)
{
String ^fileName = openFileDialog1->FileName;
IO::StreamReader ^myMazeFile = gcnew IO::StreamReader(fileName);
String ^content = myMazeFile->ReadToEnd();
richTextBox1->Text = content;
myMazeFile->Close();
// display button for open second form wich draw maze
button2->Visible = true;
}
}
now we have our file content, so we pass it to a second form who will draw the maze :
private: System::Void button2_Click(System::Object^ sender, System::EventArgs^ e)
{
String ^content = richTextBox1->Text;
Maze ^frm = gcnew Maze(content);
frm->Show();
}
Second window, create overload constructor :
Maze(String ^contentMap)
{
InitializeComponent();
String ^dimension = getWords(contentMap, 2);
array<String ^> ^coordsString = dimension->Split(gcnew array<Char> {' '});
m_width = Convert::ToInt32(coordsString[0]);
m_height = Convert::ToInt32(coordsString[1]);
panel1->Width = m_width;
panel1->Height = m_height;
}
getWords method :
String ^getWords(String ^input, int numWords)
{
try
{
int words = numWords;
for (int i = 0; i < input->Length; ++i)
{
if (input[i] == ' ' ||input[i] == '\n')
words--;
if (words == 0)
{
return input->Substring(0, i);
}
}
}
catch (Exception ^ex)
{
// ...
}
return String::Empty;
}
You have your dimension in full .Net (private member m_width and m_height).