Test Condition for a Boost Function in c++ - c++

The boost function rowclicked refers to a pointer to a function that returns a void and takes an integer as a parameter. I am trying to pass an IF condition for this pointer, basically saying if rowclicked gets activated,I wish to assign some values
boost::function<void(int)> rowClicked;
userClass->getTable()- >rowClicked=boost::bind(&UserController::onCellTableclicked,this);
this is the If condition
if(userClass->getTable()->rowClicked)
{
std::cout<<"Row Clicked";
m=i;n=j;
}
}
What changes do I make so as to pass an IF condition for a boost function that has been binded already in the class constructor..
userClass->getTable()->rowClicked=boost::bind(&UserController::onCellTableclicked,this);
Fullcode from one of the main cpp files
:
#include "UserController.h"
// needed to bind the boost function
#include <boost/bind.hpp>
#include <boost/function.hpp>
#include "UserClass.h"
// needed for used classes
#include <SUIButton.h>
#include <SUIProgressBar.h>
#include <SUIDialog.h>
#include <SUILabel.h>
#include <SUIGraphicsView.h>
#include <SUITableWidget.h>
// needed for the timer
#include <SUITimer.h>
//needed for time
#include <SUITime.h>
//needed for date
#include <SUIDate.h>
//needed for date time
#include <SUIDateTime.h>
//void getMonth( int,UserClass*);
//int co=1;
int cou=1;
int m,n;
// needed for output
#include <iostream>
UserController::UserController(UserClass *userClass) :
userClass(userClass), // assign the instance of userClass
timer(SUI::Timer::createTimer()), // instantiate the timer
timeTimer(SUI::Timer::createTimer()),
time(SUI::Time::createTime()),
date(SUI::Date::createDate())
{
// bind the Button clicked callback
userClass->getStartButton()->clicked = boost::bind(&UserController::onStartButtonClicked,this);
userClass->getStopButton()->clicked = boost::bind(&UserController::onStopButtonClicked,this);
// bind the ProgreimeoutssBar valueChanged callback
userClass->getProgressBar()->progressChanged = boost::bind(&UserController::onProgressBarValueChanged,this);
//std::cout<<userClass->getTable()->cellClicked;
//std::cout<<row;
userClass->getTable()->rowClicked=boost::bind(&UserController::onCellTableclicked,this);
//std::cout
userClass->getleftarrow()->clicked=boost::bind(&UserController::onLeftArrowClicked,this);
userClass->getrightarrow()->clicked=boost::bind(&UserController::onRightArrowClicked,this);
// userClass->getimvimage1()->getGraphicsScene()
// setup the timer
timer->setSingleShot(true);
timer->setInterval(100);
timeTimer->setSingleShot(false);
timeTimer->setInterval(1);
// bind the Timer timeout callback
timer->timeout = boost::bind(&UserController::onTimerTimeout,this);
timeTimer->timeout = boost::bind(&UserController::onTimeTimerTimeout,this);
//start the time
time->start();
//initialise date
date->currentDate();
userClass->getDateLabel()->setText(date->toString("dd.MM.yyyy"));
date->setDate(date->getYear(),date->getMonth(),1);
//std::cout<<co;
// std::cout<<date->getDayOfWeek();
//dislay month on calendar label
int mon=date->getMonth();
//enum month={"January"=1,"February","March","April","May","June","July","August","September","October","November","December"};
switch(mon)
{
case 1: userClass->getCalendarLabel()->setText("January");break;
case 2: userClass->getCalendarLabel()->setText("February");break;
case 3: userClass->getCalendarLabel()->setText("March");break;
case 4: userClass->getCalendarLabel()->setText("April");break;
case 5: userClass->getCalendarLabel()->setText("May");break;
case 6: userClass->getCalendarLabel()->setText("June");break;
case 7: userClass->getCalendarLabel()->setText("July");break;
case 8: userClass->getCalendarLabel()->setText("August");break;
case 9: userClass->getCalendarLabel()->setText("September");break;
case 10: userClass->getCalendarLabel()->setText("October");break;
case 11: userClass->getCalendarLabel()->setText("November");break;
case 12: userClass->getCalendarLabel()->setText("December");break;
}
//int mo=date->getMonth();
//getMonth(mo);
userClass->getyearlabel()->setText(std::to_string(date->getYear()));
int c=date->getDayOfWeek();
int count=1;
int i=1;
for(int j=c;j<=6;j++)
{
userClass->getTable()->setItemText(i,j,std::to_string(count));
count++;
}
for (int i=2;i<=6;i++)
{
for(int j=0;j<=6 && count<=date->getDaysInMonth();j++)
{
userClass->getTable()->setItemText(i,j,std::to_string(count));
count++;
if(count<=date->getDaysInMonth())
continue;
else break;
/* if(userClass->getTable()->rowClicked)
{
std::cout<<"Row Clicked";
m=i;n=j;
}*/
}
}
//userClass->getimvimage1()->getGraphicsScene(GraphicsScene)
//show the dialog
userClass->getDialog()->show();
userClass->getStopButton()->setEnabled(false);
}
UserController::~UserController()
{
delete userClass;
}
void UserController::onStopButtonClicked() {
std::cout << "Stop clicked" << std::endl;
// stop the timers
timer->stop();
timeTimer->stop();
// enable/disable the buttons
userClass->getStopButton()->setEnabled(false);
userClass->getStartButton()->setEnabled(true);
}
void UserController::onProgressBarValueChanged() {
// spam output when the ProgressBar value changed
std::cout << "ProgressBar value changed to: " << userClass->getProgressBar()->getValue() << std::endl;
}
void UserController::onTimerTimeout() {
// when the timer timed out increase the value of the ProgressBar with 1
userClass->getProgressBar()->setValue(userClass->getProgressBar()->getValue() + 1);
// if the ProgressBar value is smaller than 75 restart the timer
if (userClass->getProgressBar()->getValue() < 75)
timer->start();
}
void UserController::onTimeTimerTimeout() {
// every interval, add 1 msec
time->addMSecs(1);
timer->setSingleShot(true);
// set the label text
userClass->getTimeLabel()->setText(time->toString("HH:mm:ss.zzz"));
}
void UserController::onStartButtonClicked() {
// on clicked start the timer
std::cout << "Start clicked" << std::endl;
timer->start();
timeTimer->start();
// restart the time to current time
time->restart();
// enable/disable the buttonsuserClass->getTimeLabel()->setText(time->toString("HH:mm:ss.zzz"));
userClass->getStopButton()->setEnabled(true);
userClass->getStartButton()->setEnabled(false);
}
void UserController::onLeftArrowClicked()
{
std::cout<<"Left Arrow"<<std::endl;
for(int i=1;i<=6;i++)
{
for(int j=0;j<=6;j++)
{
userClass->getTable()->setItemText(i,j," ");
}
}
int mo=date->getMonth()-1;
int year=date->getYear();
if(mo==0)
{
year=date->getYear()-1;
mo=12;
}
date->setDate(year,mo,1);
//std::cout<<mo<<std::endl;
int c=date->getDayOfWeek();
int count=1;
int i=1;
for(int j=c;j<=6;j++)
{
userClass->getTable()->setItemText(i,j,std::to_string(count));
count++;
}
for (int i=2;i<=6;i++)
{
for(int j=0;j<=6 && count<=date->getDaysInMonth();j++)
{
userClass->getTable()->setItemText(i,j,std::to_string(count));
count++;
if(count<=date->getDaysInMonth())
continue;
else break;
}
}
switch(mo)
{
case 1: userClass->getCalendarLabel()->setText("January");break;
case 2: userClass->getCalendarLabel()->setText("February");break;
case 3: userClass->getCalendarLabel()->setText("March");break;
case 4: userClass->getCalendarLabel()->setText("April");break;
case 5: userClass->getCalendarLabel()->setText("May");break;
case 6: userClass->getCalendarLabel()->setText("June");break;
case 7: userClass->getCalendarLabel()->setText("July");break;
case 8: userClass->getCalendarLabel()->setText("August");break;
case 9: userClass->getCalendarLabel()->setText("September");break;
case 10: userClass->getCalendarLabel()->setText("October");break;
case 11: userClass->getCalendarLabel()->setText("November");break;
case 12: userClass->getCalendarLabel()->setText("December");break;
}
userClass->getyearlabel()->setText(std::to_string(date->getYear()));
}
void UserController::onRightArrowClicked()
{
std::cout<<"Right Arrow"<<std::endl;
for(int i=1;i<=6;i++)
{
for(int j=0;j<=6;j++)
{
userClass->getTable()->setItemText(i,j," ");
}
}
int mo=date->getMonth()+1;
int year=date->getYear();
if(mo==13)
{
year=date->getYear()+1;
mo=1;
}
date->setDate(year,mo,1);
date->setDate(date->getYear(),mo,1);
int c=date->getDayOfWeek();
int count=1;
int i=1;
for(int j=c;j<=6;j++)
{
userClass->getTable()->setItemText(i,j,std::to_string(count));
count++;
}
for (int i=2;i<=6;i++)
{
for(int j=0;j<=6 && count<=date->getDaysInMonth();j++)
{
userClass->getTable()->setItemText(i,j,std::to_string(count));
count++;
if(count<=date->getDaysInMonth())
continue;
else break;
}
}
switch(mo)
{
case 1: userClass->getCalendarLabel()->setText("January");break;
case 2: userClass->getCalendarLabel()->setText("February");break;
case 3: userClass->getCalendarLabel()->setText("March");break;
case 4: userClass->getCalendarLabel()->setText("April");break;
case 5: userClass->getCalendarLabel()->setText("May");break;
case 6: userClass->getCalendarLabel()->setText("June");break;
case 7: userClass->getCalendarLabel()->setText("July");break;
case 8: userClass->getCalendarLabel()->setText("August");break;
case 9: userClass->getCalendarLabel()->setText("September");break;
case 10: userClass->getCalendarLabel()->setText("October");break;
case 11: userClass->getCalendarLabel()->setText("November");break;
case 12: userClass->getCalendarLabel()->setText("December");break;
}
userClass->getyearlabel()->setText(std::to_string(date->getYear()));
}
/*void getMonth( int mon,UserClass *userClass)
{
switch(mon)
{
case 1: userClass->getCalendarLabel()->setText(date->toString("January"));break;
case 2: userClass->getCalendarLabel()->setText(date->toString("February"));break;
case 3: userClass->getCalendarLabel()->setText(date->toString("March"));break;
case 4: userClass->getCalendarLabel()->setText(date->toString("April"));break;
case 5: userClass->getCalendarLabel()->setText(date->toString("May"));break;
case 6: userClass->getCalendarLabel()->setText(date->toString("June"));break;
case 7: userClass->getCalendarLabel()->setText(date->toString("July"));break;
case 8: userClass->getCalendarLabel()->setText(date->toString("August"));break;
case 9: userClass->getCalendarLabel()->setText(date->toString("September"));break;
case 10: userClass->getCalendarLabel()->setText(date->toString("October"));break;
case 11: userClass->getCalendarLabel()->setText(date->toString("November"));break;
case 12: userClass->getCalendarLabel()->setText(date->toString("December"));break;
}
}*/
void UserController::onCellTableclicked() {
//date->currentDate();
//std::cout<<(userClass->getTable()->isEnabled());
// std::cout<<"Just Print:"<<cou<<std::endl;
// cou++;
// userClass->getTable()->clearFocus();
// userClass->getTable()->getCellID(int row, int col);
// int day=userClass->getTable()->getItemText(row,col);
//userClass->getTable()->toBaseWidget(this)->getWidget()->setText("Cl");
//ObjectFactory::getInstance()->toBaseWidget(this)->getWidget()->hasFocus();
// getWidget()->currentIndex().row()
//std::cout<<userClass->getTable()->rowClicked();
//std::string day=userClass->getTable()->getItemText(m,n);
userClass->getTable()->
//userClass->getTable()->currentIndex().row();
//std::cout<<getWidget()->currentIndex().row();
//date->setDate(1991,1,std::atoi(day.c_str()));
//userClass->getDateLabel()->setText(date->toString("dd.MM.yyyy"));
//userClass->getTable()->getWidget()->setText("Cl");
}

Related

How can go back to the top of my code without using loop?

I'm trying to make a "Flames" program and my problem is after the user enters his/her name accompanied with the partners name and gets the result i want to ask the user if they want to enter another name without using loop .
I've tried using goto but then errors occur where the user cant enter the initial name.
#include <iostream>
#include <string>
using namespace std;
int main()
{
start:
system("cls");
char t;
string fname1,fname2;
int i,x,z,y,name1len=0,name2len=0,b,total,name1spc=0,name2spc=0;
cout<<"Enter your full name: ";
getline(cin,fname1);
cout<<"Enter partners name: ";
getline(cin,fname2);
cout<<endl;
for(i=0;i<=fname1.length();i++ )
{
fname1[i]=toupper(fname1[i]);
}
for(i=0;i<=fname2.length();i++)
{
fname2[i]=toupper(fname2[i]);
}
x=fname1.length();
y=fname2.length();
//============check for spaces=========
for(i=0;i<x;i++)
{
if(fname1[i]==' ')
{
name1spc++;
}
}
for(i=0;i<y;i++)
{
if(fname2[i]==' ')
{
name2spc++;
}
}
//=========for 1st name compare=========
for(i=0;i<fname1.length();i++)
{
for(b=0;b<fname2.length();b++)
{
//cout<<fname1[i]<<endl;
if(fname1[i]==fname2[b])
{
name1len++;
break;
}
}
}
//============2nd name compare================
for(i=0;i<fname2.length();i++)
{
for(b=0;b<fname1.length();b++)
{
//cout<<fname1[i]<<endl;
if(fname2[i]==fname1[b])
{
name2len++;
break;
}
}
}
name1len=name1len-name1spc;
name2len=name2len-name2spc;
cout<<name1len<<endl<<name2len;
total=name1len+name2len;
if(total>6)
{
total=total%6;
}
switch(total)
{
case 1:
cout<<fname1<<" and "<<fname2<<" are Friends"<<endl;
break;
case 2:
cout<<fname1<<" and "<<fname2<<" are Lovers"<<endl;
break;
case 3:
cout<<fname1<<" and "<<fname2<<" are Aquaintance"<<endl;
break;
case 4:
cout<<fname1<<" and "<<fname2<<" are Married"<<endl;
break;
case 5:
cout<<fname1<<" and "<<fname2<<" are Enemies"<<endl;
break;
case 6:
cout<<fname1<<" and "<<fname2<<" are Sweethearts"<<endl;
break;
default:
cout<<"No name entered"<<endl;
}
cout<<"Would you like to enter another name? Y/N?";
cin>>t;
if(t=='Y' || t=='y')
{
goto start;
}
else if(t=='N' || t=='n')
{
cout<<"THANKS"<<endl;
system("pause");
}
else{
return 0;
}
}//ending for int main
You can use while loop like below:
using namespace std;
int main()
{
while(true){
system("cls");
char t;
string fname1,fname2;
int i,x,z,y,name1len=0,name2len=0,b,total,name1spc=0,name2spc=0;
cout<<"Enter your full name: ";
cin.ignore();
getline(cin,fname1);
cout<<"Enter partners name: ";
getline(cin,fname2);
cout<<endl;
for(i=0;i<=fname1.length();i++ )
{
fname1[i]=toupper(fname1[i]);
}
for(i=0;i<=fname2.length();i++)
{
fname2[i]=toupper(fname2[i]);
}
x=fname1.length();
y=fname2.length();
//============check for spaces=========
for(i=0;i<x;i++)
{
if(fname1[i]==' ')
{
name1spc++;
}
}
for(i=0;i<y;i++)
{
if(fname2[i]==' ')
{
name2spc++;
}
}
//=========for 1st name compare=========
for(i=0;i<fname1.length();i++)
{
for(b=0;b<fname2.length();b++)
{
//cout<<fname1[i]<<endl;
if(fname1[i]==fname2[b])
{
name1len++;
break;
}
}
}
//============2nd name compare================
for(i=0;i<fname2.length();i++)
{
for(b=0;b<fname1.length();b++)
{
//cout<<fname1[i]<<endl;
if(fname2[i]==fname1[b])
{
name2len++;
break;
}
}
}
name1len=name1len-name1spc;
name2len=name2len-name2spc;
cout<<name1len<<endl<<name2len;
total=name1len+name2len;
if(total>6)
{
total=total%6;
}
switch(total)
{
case 1:
cout<<fname1<<" and "<<fname2<<" are Friends"<<endl;
break;
case 2:
cout<<fname1<<" and "<<fname2<<" are Lovers"<<endl;
break;
case 3:
cout<<fname1<<" and "<<fname2<<" are Aquaintance"<<endl;
break;
case 4:
cout<<fname1<<" and "<<fname2<<" are Married"<<endl;
break;
case 5:
cout<<fname1<<" and "<<fname2<<" are Enemies"<<endl;
break;
case 6:
cout<<fname1<<" and "<<fname2<<" are Sweethearts"<<endl;
break;
default:
cout<<"No name entered"<<endl;
}
cout<<"Would you like to enter another name? Y/N?";
cin>>t;
if(t=='Y' || t=='y')
{
continue;
}
else if(t=='N' || t=='n')
{
cout<<"THANKS"<<endl;
break;
}
else{
return 0;
}
} //while end
}//ending for int main

'Pointer to member' error C++

I need some help with this error, I don't understand what has to be done to get rid of it. I am trying to build this code - the last exercise, Graduation. Here is my code:
// Bunnies.cpp : Defines the entry point for the console application.
// Male = 0; Female = 1
#include "stdafx.h"
#include <stdlib.h>
#include <iostream>
#include<stdio.h>
#include<time.h>
using namespace std;
int num;
class bunny {
int val;
int gender;
int colour;
int age;
int type;
bool radioBun;
string name;
public:
void create() {
val = num;
age = 1;
gender = rand() % 2;
switch (rand() % 100) {
case 1: radioBun = true;
break;
case 2: radioBun = true;
break;
default: radioBun = false;
break;
}
if (gender = 0) {
switch (rand() % 20) {
case 0: name = "Bob";
break;
case 1: name = "Alexis";
break;
case 2: name = "William";
break;
case 3: name = "Cleo";
break;
case 4: name = "Mark";
break;
case 5: name = "Jarod";
break;
case 6: name = "Billie";
break;
case 7: name = "Nathan";
break;
case 8: name = "Richard";
break;
case 9: name = "Thomas";
break;
case 10: name = "Rudolf";
break;
case 11: name = "Troy";
break;
case 12: name = "Wesley";
break;
case 13: name = "Jacob";
break;
case 14: name = "Cody";
break;
case 15: name = "Gavin";
break;
case 16: name = "Norris";
break;
case 17: name = "Matt";
break;
case 18: name = "Colton";
break;
case 19: name = "Daniel";
break;
}
}
else {
switch (rand() % 20) {
case 0: name = "Cecila";
break;
case 1: name = "Scarlet";
break;
case 2: name = "Abby";
break;
case 3: name = "Sandra";
break;
case 4: name = "Melissa";
break;
case 5: name = "Lizabeth";
break;
case 6: name = "Susie";
break;
case 7: name = "Cherly";
break;
case 8: name = "Kaitlin";
break;
case 9: name = "Debbie";
break;
case 10: name = "Evalyn";
break;
case 11: name = "Amalia";
break;
case 12: name = "Mendy";
break;
case 13: name = "Nora";
break;
case 14: name = "Brigitte";
break;
case 15: name = "Ebony";
break;
case 16: name = "Beatrice";
break;
case 17: name = "Tiffany";
break;
case 18: name = "Ying";
break;
case 19: name = "Kesha";
break;
}
}
}
void printCreate() {
cout << "Bunny ";
cout << val;
cout << " was zapped into existence!" << endl;
}
};
int main()
{
srand(time(0));
for (int x = 0; x == 10; x++) {
num = x;
bunny num;
num.create();
num.printCreate;
}
system("pause");
return 0;
}
I am working out a way to create 10 bunnies at the start of the code, by creating a variable num. The for loop runs 10 times, everytime increasing num by +1. Each time creating a new instance of the class bunnies and assigning the value of num to the name of that instance.
The void create() is not giving me any problems, its when I try to do void printCreate() that I get this error. It just comes up with "Severity Code Description Project File Line Suppression State
Error C3867 'bunny::printCreate': non-standard syntax; use '&' to create a pointer to member Bunnies c:\users\bob\source\repos\bunnies\bunnies\bunnies.cpp 145
". I have looked this up, but I can't understand how to fix this error. Could someone please tell me how to fix this in simple terms?
Replace num.printCreate; in your loop by num.printCreate();.

C++ values not reading in while loop switch case

All functions have be declared public in my .header file
this is my code:
void menu
{
int option = 0;
//cout menu here//
//getting user input to enter their option of menu
cin>>option;
menuChoice(option);
}
void menuChoice(int option)
{
int kiv = 0;
const int size = 50;
while(option) //option just an int to get input from a menu
{
switch(option)
{
case 1: {
while(kiv<size)
{
//codes here to read user inputs then entry increment
++kiv;
}
//cout<<kiv<<endl; // doesn't work here too , it displays nothing
}break;
case2:{
cout<<kiv<<endl; // displays 0
}break;
}
}
}
i have declared the kiv on top , then increment it in the while loop , however the value stays in the while(option) loop so when i go to case 2 , it suppose to print out the incremented value , but it display 0 , am i going in the correct direction?
This may be the one you looking for
int kiv = 0;
const int size = 50;
void menuChoice(int option);
void getOption()
{
int option = 0;
cin >> option;
menuChoice(option);
}
void menuChoice(int option)
{
switch (option) {
case 1:
while (kiv < size) {
//codes here to read user inputs then entry increment
++kiv;
}
//cout<<kiv<<endl; // doesn't work here too , it displays nothing
break;
case 2:
cout << kiv << endl; // displays 0
break;
}
getOption();
}
void menu()
{
//cout menu here//
//getting user input to enter their option of menu
getOption();
}
int main(int argc, char **argv)
{
menu();
return 0;
}
It's not entirely clear from your example, but I am guessing that each time you process a menuChoice you enter that function, and as a result you re-initialised kiv back to 0 each time.
If that is the case, you probably need to change the type of kiv to
static int kiv = 0;
This will cause kiv to retain its value between invocations.

Sorting array of pointers

I am trying to sort an array of pointers, which are pointing to a character array of names. I can get the program to sort the first two names but not the rest. Also the names are part of a structure.
void sort( Class* ptrs[], int num_classes){
bool swap=false;
do
{
swap=false;
for (int i=0; i<=num_classes-1; i++) {
if (ptrs[i]->title[i]>ptrs[i+1]->title[i]) {
Swap(&ptrs[i], &ptrs[i+1]);
swap=true;
}
}
}while(swap);
}
void Swap(Class** num1,Class** num2){
Class* temp=*num1;
*num1=*num2;
*num2=temp;
}
struct Class{
char title[MAX];
int units;
char grade;
};
int main(){
int choice,num_classes=0;
char class_selection[MAX];
Class* ptrs[MAX];
char* class_ptr[MAX];
bool Continue=false;
Class classes[MAX];
for (int i = 0; i < MAX; i++){
ptrs[i]=&classes[i];
}
cout<<" 1. Add new class"<<endl<<" 2. Edit an existing class"<<endl<<" 3. Display a class"<<endl<<" 4. List all classes"<<endl<<" 5. Display GPA"<<endl<<" 6. Delete all classes"<<endl<<" 7. Quit"<<endl<<"Enter selection number: ";
cin>>choice;
if (choice!=7) {
Continue=true;
}
switch (choice) {
case 1:
add(ptrs,num_classes);
num_classes++;
break;
case 2:
edit(ptrs,num_classes);
cin.ignore();
break;
case 3:
sort(ptrs,num_classes);
cin.ignore();
cout<<"Enter the name of the class to display: ";
cin.getline(class_selection, MAX);
for (int j =0; j<MAX; j++) {
class_ptr[j]=&class_selection[j];
}
Bin_search(ptrs,num_classes,class_ptr);
break;
case 4:
sort(ptrs,num_classes);
display(ptrs,num_classes);
break;
case 5:
// display_GPA();
break;
case 6:
// delete_end();
break;
}
if (choice == 7) {
// delete_end();
//system('pause");
return 0;
}
}
This code is comparing i-th character of corresponding titles, not the whole titles:
if (ptrs[i]->title[i]>ptrs[i+1]->title[i]) {
// ^^^ ^^^
Possible ways to solve:
Change this
char title[MAX];
to
std::string title;
And after that you'll be able to write
if (ptrs[i]->title > ptrs[i+1]->title) {
Only if you can't use std::string for some reason, use strcmp:
if (strcmp(ptrs[i]->title, ptrs[i+1]->title) > 0) {

why does my method not read float type from file .txt

my input file:
QT, Quan Tri, 4
KT, Kinh Te, 4
DT, Dien Tu, 5
MT, My Thuat, 4.5
NN, Ngoai Ngu, 4
my method:
void ReadFileDT(fstream &file)
{
char c;
CarneerDT dt;
int i=0;
int t=0;// position attributes
int k=0;
char number[10];
while(!file.eof())
{
c=file.get(); //get a letter in the file
//end of line processing
if(c=='\n')
{
break;
}
if(c!=',')
{
switch(t)
{
case 0: dt.id[i++]=c;break;
case 1: number[k++]=c;break;
case 2: number[k]=c;number[k+1]='\0';dt.year=atof(number);t++;break;
}
}
else
{
switch(t)
{
case 0:dt.id[i]='\0';break;
case 1:number[k]='\0';strcpy(dt.name,number); k=0;break;
}
t++;
}
}
cout<<endl;
XuatDT(dt);
}
void ReadAllDT(char *tenfile)
{
char c;
fstream file;
file.open("NganhDT.txt",ios::in);
if(file.fail())
{
cout<<"File Error!";
exit(1);
}
while(!file.eof())
{
ReadFileDT(file);
}
file.close();
}
it show "Run-Time Check Failure #2 - Stack around the variable 'number' was corrupted." and year always equal to 0.00000000000. Was I wrong in line:"case 2: number[k]=c;number[k+1]='\0';dt.year=atof(number);t++;break;"?
This code is structured slightly wrong
if(c!=',')
{
switch(t)
{
case 0: dt.id[i++]=c;break;
case 1: number[k++]=c;break;
case 2: number[k]=c;number[k+1]='\0';dt.year=atof(number);t++;break;
}
}
else
{
switch(t)
{
case 0:dt.id[i]='\0';break;
case 1:number[k]='\0';strcpy(dt.name,number); k=0;break;
}
t++;
}
it should be
if(c!=',')
{
switch(t)
{
case 0: dt.id[i++]=c;break;
case 1: number[k++]=c;break;
case 2: number[k++]=c;break;
}
}
else
{
switch(t)
{
case 0:dt.id[i]='\0';break;
case 1:number[k]='\0';strcpy(dt.name,number); k=0;break;
case 2: number[k]='\0';dt.year=atof(number); k=0;break;
}
t++;
}
But I must admit I don't immediately see why your code would lead to stack corruption.