I'm a newbie in C++ Builder and really need some help. In my work, we have to use C++ Builder and I can't find very much documentation about it.
What I want to do is to insert a new row with some data in each column each time the user press the Insert key. There are 20 rows in total in my TGrid: 1 TCheckColumn and 19 TStringColumn.
I don't know how to do it exactly. First, here is the code I already wrote when someone enter the insert key:
void __fastcall TForm2::Grid1KeyDown(TObject *Sender, WORD &Key, System::WideChar &KeyChar,
TShiftState Shift)
{
switch(Key)
{
case VK_INSERT:
if(Grid1->RowCount>MAXTask){}else
{
Label1->Text = "number Task: "+IntToStr(++nmbertask);
Grid1->RowCount++;
}
break;
}
}
Here is the Grid1GetValue() and Grid1SetValue() code:
void __fastcall TForm2::Grid1GetValue(TObject *Sender, const int Col, const int Row,
TValue &Value)
{
switch(Col)
{
case 0:
Value = A[Col][Row]; break;
case 1:
Value = A[Col][Row]; break;
case 2:
Value = A[Col][Row]; break;
case 3:
Value = A[Col][Row]; break;
case 4:
Value = A[Col][Row]; break;
case 5:
Value = A[Col][Row]; break;
case 6:
Value = A[Col][Row]; break;
case 7:
Value = A[Col][Row]; break;
case 8:
Value = A[Col][Row]; break;
case 9:
Value = A[Col][Row];break;
case 10:
Value = A[Col][Row];break;
case 11:
Value = A[Col][Row];break;
case 12:
Value = A[Col][Row];break;
case 13:
Value = A[Col][Row];break;
case 14:
Value = A[Col][Row];break;
case 15:
Value = A[Col][Row];break;
case 16:
Value = A[Col][Row];break;
case 17:
Value = A[Col][Row];break;
case 18:
Value = A[Col][Row]; break;
case 19:
Value = A[Col][Row];break;
}
}
void __fastcall TForm2::Grid1SetValue(TObject *Sender, const int Col, const int Row,
const TValue &Value)
{
switch(Col)
{
case 0:
A[Col][Row] = Value; break;
case 1:
A[Col][Row] = Value; break;
case 2:
A[Col][Row] = Value; break;
case 3:
A[Col][Row] = Value; break;
case 4:
A[Col][Row] = Value; break;
case 5:
A[Col][Row] = Value; break;
case 6:
A[Col][Row] = Value; break;
case 7:
A[Col][Row] = Value; break;
case 8:
A[Col][Row] = Value; break;
case 9:
A[Col][Row] = Value; break;
case 10:
A[Col][Row] = Value; break;
case 11:
A[Col][Row] = Value; break;
case 12:
A[Col][Row] = Value; break;
case 13:
A[Col][Row] = Value; break;
case 14:
A[Col][Row] = Value; break;
case 15:
A[Col][Row] = Value; break;
case 16:
A[Col][Row] = Value; break;
case 17:
A[Col][Row] = Value; break;
case 18:
A[Col][Row] = Value; break;
case 19:
A[Col][Row] = Value; break;
}
}
This is what I've done so far. I've never developed in C++ Builder, so can you help me please? I'm not really looking for a written solution, I'm really looking for an indication of where to look for a solution.
If I'm not clear, please indicate it so I can give more information. If you also have a good site with good documentation about C++ Builder, please indicate it to me. English is not my native language but I will do my best.
You can find the TGrid documentation on Embarcadero's DocWiki. Look at the 'Methods' page to figure out how to insert and remove elements in your TGrid. There are two methods that can help you: InsertComponent() and `InsertObject().
Also, in your Grid1GetValue() and Grid1SetValue() methods, you are using a switch statement. In all those cases you are doing the same job :
Value = A[Col][Row];break;
So, instead of using a switch, you can simply call one time Value = A[Col][Row]; and it will do the same result.
I hope my answer can help you. At least I answered what I understood from your question.
Related
On the menu deposits n,d, q, o,f are indicated which are indication for nickel dime quarter , one dollar and five dollar. I should also indicate "C" as in cancel option when the user hits c. But i couldn't make it work. My program still runs even the user hit c. I am confused on this point?
switch(DepositIndication) {
case 'n': {
PurchasedPrice=PurchasedPrice-0.05;
NumOfNickels=NumOfNickels+1;
}
break;
case 'd': {
PurchasedPrice=PurchasedPrice-0.10;
NumOfdimes=NumOfdimes+1;
}
break;
case 'q': {
PurchasedPrice=PurchasedPrice-0.25;
NumOfquarters=NumOfquarters+1;
}
break;
case 'o': {
PurchasedPrice=PurchasedPrice-1.00;
NumOfOnes=NumOfOnes+1;
}
break;
case 'f': {
PurchasedPrice=PurchasedPrice-5.00;
NumOfFives=NumOfFives+1;
}
break;
}
the condition c is missing and I Put the break into to brackets;
switch (DepositIndication) {
case 'n':
{
PurchasedPrice = PurchasedPrice - 0.05;
NumOfNickels = NumOfNickels + 1;
break;
}
case 'd':
{
PurchasedPrice = PurchasedPrice - 0.10;
NumOfdimes = NumOfdimes + 1;
break;
}
case 'q':
{
PurchasedPrice = PurchasedPrice - 0.25;
NumOfquarters = NumOfquarters + 1;
break;
}
case 'o':
{
PurchasedPrice = PurchasedPrice - 1.00;
NumOfOnes = NumOfOnes + 1;
break;
}
case 'f':
{
PurchasedPrice = PurchasedPrice - 5.00;
NumOfFives = NumOfFives + 1;
break;
}
case 'c':
}
//you can print cancellation messages in here
break;
}
default:
break;
}
I should also indicate "C" as in cancel option when the user hits c
Your current code works fine the only thing you are missing to make that work right now is a default case or a case where you specifically check if the user inputed C.
Example:
switch(DepositIndication) {
// All other cases ...
case 'c': {
// The user canceled the interaction
std::cout << "Interaction was canceled." << std::endl;
break;
}
// Default case to ensure that even,
// if the user hits any other key we still handle his interaction
default {
std::cout << "No fitting interactional behaviour found." << std::endl;
break;
}
}
If you want to ensure that the user can even input big lettered chars and the switch case will still work you can use std::tolower and cast it back to a char.
Make the User-Input lowercase:
// Make DepositIndication lowercase
DepositIndication = std::tolower(DepositIndication, std::locale());
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();.
I want to know whether the following is possible in C++. I need to construct a class that will store as a data member a std::map, with keys being of type std::string, and values being function pointers. The thing is that I want these function pointers to be variadic in the sense that they should point to functions accepting an arbitrary number of arguments, i.e. pointing to functions of the form
template<class... Args>
f(Args...);
The important bit is that I want to be able to have different arguments for the different function pointers in the map of a given instance of my class. For example, I might want to create an object of my class and have its map contain two pairs, one corresponding to a function having (double, int) as arguments, and another having (std::vector, int, int) as arguments. And I want to be able to make this general, in the sense that I want to be able to add new elements to the map, with possibly different argument lists (although I would only do this at compile-time, I still need to code the class without knowing about the types since I want to add the new elements from other files/clients).
What is the best way to implement this?
For all those saying you cant, you actually can, it's not pretty:
This is an example code from the output of the moc tool, that does exactly that: It stores an arbitrary amount of function / method pointers, with an arbitrary number of arguments.
Easiest Solution: Just use Qt's moc tool to generate that for you,
If you cannot or don't want to use Qt, you still can analize the code below on how they achieve it.
int AtCore::qt_metacall(QMetaObject::Call _c, int _id, void **_a)
{
_id = QObject::qt_metacall(_c, _id, _a);
if (_id < 0)
return _id;
if (_c == QMetaObject::InvokeMetaMethod) {
if (_id < 25)
qt_static_metacall(this, _c, _id, _a);
_id -= 25;
} else if (_c == QMetaObject::RegisterMethodArgumentMetaType) {
if (_id < 25)
*reinterpret_cast<int*>(_a[0]) = -1;
_id -= 25;
}
return _id;
}
void AtCore::qt_static_metacall(QObject *_o, QMetaObject::Call _c, int _id, void **_a)
{
if (_c == QMetaObject::InvokeMetaMethod) {
AtCore *_t = static_cast<AtCore *>(_o);
Q_UNUSED(_t)
switch (_id) {
case 0: _t->printProgressChanged((*reinterpret_cast< const float(*)>(_a[1]))); break;
case 1: _t->receivedMessage((*reinterpret_cast< const QByteArray(*)>(_a[1]))); break;
case 2: _t->stateChanged((*reinterpret_cast< PrinterState(*)>(_a[1]))); break;
case 3: _t->print((*reinterpret_cast< const QString(*)>(_a[1]))); break;
case 4: _t->stop(); break;
case 5: _t->pause((*reinterpret_cast< const QString(*)>(_a[1]))); break;
case 6: _t->resume(); break;
case 7: _t->home((*reinterpret_cast< uchar(*)>(_a[1]))); break;
case 8: _t->home(); break;
case 9: _t->setExtruderTemp((*reinterpret_cast< uint(*)>(_a[1])),(*reinterpret_cast< uint(*)>(_a[2]))); break;
case 10: _t->setExtruderTemp((*reinterpret_cast< uint(*)>(_a[1]))); break;
case 11: _t->setExtruderTemp(); break;
case 12: _t->move((*reinterpret_cast< uchar(*)>(_a[1])),(*reinterpret_cast< uint(*)>(_a[2]))); break;
case 13: _t->setBedTemp((*reinterpret_cast< uint(*)>(_a[1]))); break;
case 14: _t->setBedTemp(); break;
case 15: _t->setFanSpeed((*reinterpret_cast< uint(*)>(_a[1])),(*reinterpret_cast< uint(*)>(_a[2]))); break;
case 16: _t->setFanSpeed((*reinterpret_cast< uint(*)>(_a[1]))); break;
case 17: _t->setFanSpeed(); break;
case 18: _t->setAbsolutePosition(); break;
case 19: _t->setRelativePosition(); break;
case 20: _t->setPrinterSpeed((*reinterpret_cast< uint(*)>(_a[1]))); break;
case 21: _t->setPrinterSpeed(); break;
case 22: _t->setFlowRate((*reinterpret_cast< uint(*)>(_a[1]))); break;
case 23: _t->setFlowRate(); break;
case 24: _t->close(); break;
default: ;
}
} else if (_c == QMetaObject::IndexOfMethod) {
int *result = reinterpret_cast<int *>(_a[0]);
void **func = reinterpret_cast<void **>(_a[1]);
{
typedef void (AtCore::*_t)(const float & );
if (*reinterpret_cast<_t *>(func) == static_cast<_t>(&AtCore::printProgressChanged)) {
*result = 0;
return;
}
}
{
typedef void (AtCore::*_t)(const QByteArray & );
if (*reinterpret_cast<_t *>(func) == static_cast<_t>(&AtCore::receivedMessage)) {
*result = 1;
return;
}
}
{
typedef void (AtCore::*_t)(PrinterState );
if (*reinterpret_cast<_t *>(func) == static_cast<_t>(&AtCore::stateChanged)) {
*result = 2;
return;
}
}
}
}
I am developing an Android client for Microsoft Dynamics NAV. I am using ksoap2 for making communication with NAV webservices. I used MarshalDate class to serialize date. When I pass Date to webservice I am getting an error like "String was not recognized as a valid DateTime."
MarshalDate.java
public class MarshalDate implements Marshal {
public static Class DATE_CLASS = new Date().getClass();
public Object readInstance(XmlPullParser parser, String namespace, String name, PropertyInfo expected) throws IOException, XmlPullParserException {
return IsoDate.stringToDate(parser.nextText(), IsoDate.DATE_TIME);
}
public void register(SoapSerializationEnvelope cm) {
cm.addMapping(cm.xsd, "dateTime", Date.class, this); }
public void writeInstance(XmlSerializer writer, Object obj) throws IOException {
writer.text(IsoDate.dateToString((Date) obj, IsoDate.DATE_TIME)); } }
My object for salesordercard,
SalesOrderCard.java
public class SalesOrderCard implements KvmSerializable
{
String Key;
String No;
String Sell_to_Customer_No;
Sales_Order_Line_List Salesorderlinelist;
String ShopNo;
Date OrderDate;
double Received_Amount;
boolean Cheque;
public SalesOrderCard(){}
public SalesOrderCard(String key,String no,String selltocustno,Sales_Order_Line_List salesorderline,String shopno,Date orderdate ,double received_Amount,boolean cheque) {
Key = key;
No = no;
Sell_to_Customer_No = selltocustno;
Salesorderlinelist=salesorderline;
ShopNo=shopno;
OrderDate=orderdate;
received_Amount=Received_Amount;
Cheque=cheque;
}
public Object getProperty(int arg0) {
switch(arg0)
{
case 0:
return Key;
case 1:
return No;
case 2:
return Sell_to_Customer_No;
case 3:
return Salesorderlinelist;
case 4:
return ShopNo;
case 5:
return Received_Amount;
case 6:
return Cheque;
case 7:
return OrderDate;
}
return null;
}
public int getPropertyCount() {
return 8;
}
public void getPropertyInfo(int index, Hashtable arg1, PropertyInfo info) {
switch(index)
{
case 0:
info.type = PropertyInfo.INTEGER_CLASS;
info.name = "Key";
break;
case 1:
info.type = PropertyInfo.INTEGER_CLASS;
info.name = "No";
break;
case 2:
info.type = PropertyInfo.INTEGER_CLASS;
info.name = "Sell_to_Customer_No";
break;
case 3:
info.type = Sales_Order_Line_List.class;
info.name = "SalesLines";
break;
case 4:
info.type = PropertyInfo.INTEGER_CLASS;
info.name = "Shop_No";
break;
case 5:
info.type = PropertyInfo.INTEGER_CLASS;
info.name = "Received_Amount";
break;
case 6:
info.type = PropertyInfo.INTEGER_CLASS;
info.name = "Cheque";
break;
case 7:
info.type = MarshalDate2.DATE_CLASS;
info.name = "Order_Date";
break;
default:break;
}
}
public void setProperty(int index, Object value) {
switch(index)
{
case 0:
Key = value.toString();
break;
case 1:
No = value.toString();
break;
case 2:
Sell_to_Customer_No = value.toString();
break;
case 3:
Salesorderlinelist = (Sales_Order_Line_List)value;
break;
case 4:
ShopNo = value.toString();
break;
case 5:
Received_Amount = Double.parseDouble(value.toString());
break;
case 6:
Cheque = Boolean.parseBoolean(value.toString());
break;
case 7:
OrderDate=(Date)value;
break;
default:
break;
}
}
}
And I am passing date like,
String DateString="22-05-2014 19:32:52";
SimpleDateFormat format=new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
d=(Date)format.parse(DateString);
SalesOrderCard cr=new SalesOrderCard();
cr.OrderDate=d;
PropertyInfo custProp = new PropertyInfo();
custProp.setName("SalesOrderCard");
custProp.setValue(cr);
custProp.setType(SalesOrderCard.class);
request.addProperty(custProp);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
MarshalDouble md = new MarshalDouble();
md.register(envelope);
MarshalDate mdate=new MarshalDate();
mdate.register(envelope);
Assuming d is a Date this code looks correct
String DateString="22-05-2014 19:32:52";
SimpleDateFormat format=new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
d=(Date)format.parse(DateString);
I suspect this line may be the problem
writer.text(IsoDate.dateToString((Date) obj, IsoDate.DATE_TIME));
If you know the format writer.text expects then I would try using SimpleDateFormat to create the string for you instead:
String dateString = format.format((Date)obj);
I was just wondering if someone could just give me an example of how to use switch/case. I kinda get the just of it but am having trouble figuring out what and how i can use this. Thank you in advance.
There are couple of things to remember about switch case statements:
a) The condition should be integeral/enum/user defined type which supports conversion to int or enum
b) case lables are compile time constants
c) No two case label expressions can have the same value
d) $6.4.2/5- "When the switch statement is executed, its condition is evaluated and compared with each case constant. If one of the case constants is equal to the value of the condition, control is passed to the statement following the matched case label. If no case constant matches the condition, and if there is a default label,control passes to the statement labeled by the default label. If no case matches and if there is no default then none of the statements in the switch is executed."
e) $6.4.2/6- "case and default labels in themselves do not alter the flow of control, which continues unimpeded across such labels. To exit from a switch, see break"
enum direction {north, south, east, west};
char x;
class UD{
operator int(){return 0;}
};
direction f1(){
return north;
}
char f2(){
return 'A';
}
int main(){
direction d = f();
string country;
// switch condition of type enum
switch(d){
case north:
country = "P";
break;
case south:
country = "Q";
break;
case east:
country = "R";
break;
case west:
country = "S";
break;
default:
country = "";
break;
}
// switch condition of integral type
switch(c){
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
cout << "Vowel";
break;
default:
cout << "Not a Vowel";
break;
}
UD u;
// switch condition of user defined type (conversion to integral type)
switch(u){
case 0:
case 1:
cout << "Good";
break;
default:
cout << "Not so good";
break;
}
}
Here is a fairly typical use case. You have a list of values (the enum) and a switch which checks the input to determine which you are dealing with. This assumes of course that the action you will take depends on the underlying value of the enum.
enum ImageFormat
{
FormatRGB888,
FormatRGB8888,
FormatRGB101010,
FormatRGB161616,
FormatRGB16161616
};
void SomeFunc(ImageFormat format)
{
switch(format)
{
case FormatRGB888:
// do stuff
break;
case FormatRGB8888:
// do stuff
break;
case FormatRGB101010,
// do stuff
break;
case FormatRGB161616:
// do stuff
break;
case FormatRGB16161616:
// do stuff
break;
default:
// bad value for 'format'
}
}
Say you have an enum
enum expr_type {
EXPR_TYPE_ADD,
EXPR_TYPE_SUBTRACT,
EXPR_TYPE_GET_VALUE
};
We can do a switch on this:
enum expr_type t = /* get input somehow and find the type */;
switch(t) {
case EXPR_TYPE_ADD:
cout << "Operator Add";
/* fall through */
case EXPR_TYPE_SUBTRACT:
cout << "Operator (Add or Subtract)";
break;
case EXPR_TYPE_GET_VALUE;
cout << "Getting some value";
break;
}
You have to put in the break; so it doesn't fallthrough - Currently, EXPR_TYPE_ADD will exute all the code for EXPR_TYPE_SUBTRACT. Make sure to use break correctly!
Switch statements are a more efficient way of doing a lot of ifs and elses.
import java.util.Scanner;
class Date{
public static void main(String[] args) {
String dow;
String wowby;
String yowby;
Double n1,n2,res;
Scanner scan = new Scanner (System.in);
System.out.print("Enter Date (dd/mm/yy): ");
String date = scan.nextLine();
String dd = date.substring(0,2);
String mm = date.substring(3,5);
String yy = date.substring(6,8);
int d = Integer.valueOf(dd);
int m = Integer.valueOf(mm);
int y = Integer.valueOf(yy);
boolean valid = (d>=1) && (d<31)||(m>=1) && (m<12);//||((y>=00) && (y<99));
if(!valid)
System.out.print("Invalid date");
else {
switch (dd)
{
case "01":
System.out.print("First of ");
switch (mm) {
case "01":
System.out.print("January,2020");
break;
case "02":
System.out.print("February,2020");
break;
case "03":
System.out.print("March,2020");
break;
case "04":
System.out.print("April,2020");
break;
case "05":
System.out.print("May,2020");
break;
case "06":
System.out.print("June,2020");
break;
case "07":
System.out.print("July,2020");
break;
case "08":
System.out.print("August,2020");
break;
case "09":
System.out.print("September,2020");
break;
case "10":
System.out.print("October,2020");
break;
case "11":
System.out.print("November,2020");
break;
case "12":
System.out.print("December,2020");
break;
default:
System.out.print(" Invalid date ");
}
break;
case "02":
System.out.print("Second of ");
switch (mm)
{
case "01":
System.out.print("January,2020");
break;
case "02":
System.out.print("February,2020");
break;
case "03":
System.out.print("March,2020");
break;
case "04":
System.out.print("April,2020");
break;
case "05":
System.out.print("May,2020");
break;
case "06":
System.out.print("June,2020");
break;
case "07":
System.out.print("July,2020");
break;
case "08":
System.out.print("August,2020");
break;
case "09":
System.out.print("September,2020");
break;
case "10":
System.out.print("October,2020");
break;
case "11":
System.out.print("November,2020");
break;
case "12":
System.out.print("December,2020");
break;
default:
System.out.print(" Invalid month ");
}
break;
case "03":
System.out.print("Third of ");
switch (mm)
{
case "01":
System.out.print("January,2020");
break;
case "02":
System.out.print("February,2020");
break;
case "03":
System.out.print("March,2020");
break;
case "04":
System.out.print("April,2020");
break;
case "05":
System.out.print("May,2020");
break;
case "06":
System.out.print("June,2020");
break;
case "07":
System.out.print("July,2020");
break;
case "08":
System.out.print("August,2020");
break;
case "09":
System.out.print("September,2020");
break;
case "10":
System.out.print("October,2020");
break;
case "11":
System.out.print("November,2020");
break;
case "12":
System.out.print("December,2020");
break;
default:
System.out.print(" Invalid month ");
}
return;
}
}
}
}