|
I mean in same way as Turbo C++ did. For C# and all are basically java. They don't do multiple inheritance as effortlessly as Turbo C++ use to.
|
|
|
|
|
Hi!
I cannot compile this program.
Please help me because I have no idea how I do it, my teacher spent this activity and I found it very difficult.
After completing an option, the program must return to the initial menu until "0" is pressed.
<pre>
#include <stdio.h>
#include <conio.h>
int main()
{
system("CLS");
int item [8][5]; int op,vaga,i,j;
float value[8][5]; float box = 0;
printf("\n");
printf ("|*--------------------Welcome to the restaurant! -------------------- *|\n");
printf("\n");
printf(" [1] Open table\n");
printf(" [2] Launch expense\n");
printf(" [3] Close table account\n");
printf(" [4] Check Cashier\n");
printf(" [0] Exit\n");
printf("\n Choose an option:");
scanf ("%d",&op);
switch (op)
{
case 0:
printf("Leaving the Program");
break;
case 1:
printf ("Enter table number:");
scanf("%d");
if (item>=-1) printf("%d table open");
else if (item<= 0)
printf("Error that table is not free. Choose another table!");
function_open_table();
break;
case 2:
printf("Value launched on table");
printf("Placement amount: %d");
scanf("%d");
printf("Total value of the table:R$ ");
scanf("%f");
if (table>=-0) printf("%d table open");
else if (table<= -1)
printf("Error that table is not open!");
function_Launch_dispatch();
break;
case 3:
printf("The table has just been closed");
break;
case 4:
printf("The total value in the box: R$%.2f");
break;
default:
printf("option invalidates!");
}
return 0;
}
|
|
|
|
|
What error(s) do you get from the compiler?
|
|
|
|
|
At a quick glance, you have this
scanf ("%d",&op);
and you have this
scanf("%d"); the second one is missing the variable where you want the data to go.
Similarly with the printf() statements
printf("Placement amount: %d"); you're specifying an integer to be output with the %d, there isn't a corresponding value.
printf("Placement amount: %d", value);
"the debugger doesn't tell me anything because this code compiles just fine" - random QA comment
"Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst
"I don't drink any more... then again, I don't drink any less." - Mike Mullikins uncle
|
|
|
|
|
I managed to compile only that now what I'm having difficulty is to make the program return to the menu at the end of the command so that new commands are given and the values are saved in the box.
(I'm sorry if my English is bad, I am translating to clear my doubts here.)
#include <stdio.h>
#include <conio.h>
int main()
{
int item [8][5]; int i,j,op;
float value[8][5]; float box = 0; int table();
printf("\n");
printf ("|*--------------------"|- Welcome to the restaurant! -------------------- *|\n");
printf("\n");
//Menu
printf(" [1] Open table\n");
printf(" [2] Launch expense\n");
printf(" [3] Close table account\n");
printf(" [4] Check Cashier\n");
printf(" [0] Exit\n");
printf("\n Choose an option:");
scanf ("%d",&op);
switch (op)
// should receive only one character which are the (1,2,3,4,0) without the need to press the enter any other number should be disregarded.
{
case 0:
printf("Leaving");
break;
case 1:
printf ("Enter table number: ");
scanf ("%d",&i,&j);
if (item <=-1) //item=-1 closed table
printf("open table %d \n ",i,j);
else //////
if (item>=0) //item =0 closed table
printf("This table is not free. Choose another table!");
break;
case 2:
printf ("Enter table number:");
scanf ("%d",&i,&j);
if (item <=0) //item =0 open table
printf ("Enter with the value to be launched on the table %d: R$ \n ",i,j);
scanf ("%.2f",&i,&j,&value);
printf(" Value launched on the table.\n ");
printf("Amount posted: \n"); // show how many postings were on the table
printf(" Total table value: %.2f \n",value);
(item >=-1);
printf(" Error: This table is not open! \n"); //item=-1 table closed
break;
case 3:
printf ("Enter table number:");
scanf ("%d",&i,&j);
printf(" The table has just been closed \n");
printf(" Amount of postings: \n"); // show how many postings you had on the table
printf(" Total value of the table %d e' R$ %.2f \n",i,j,value);
break;
case 4:
printf("The total value in the box: R$"); // should show the total value you have in the box of all tables that have already been opened
scanf("%f",&box);
break;
}
return 0;
}
|
|
|
|
|
To return to the menu I'd imagine that you would need some type of loop construct, like a while() loop, or a do-while loop. Your formatted inputs and outputs don't look right;
Member 14984894 wrote: scanf ("%d",&i,&j); you're asking for a single integer (the '%d'), but you have 2 variables there i and j. What are you expecting to happen? Same goes for some of the printf() statements.
"the debugger doesn't tell me anything because this code compiles just fine" - random QA comment
"Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst
"I don't drink any more... then again, I don't drink any less." - Mike Mullikins uncle
|
|
|
|
|
I left & i, & j because it is the line and the row where the tables will be chosen. I thought that this way I would be storing the tables that were chosen
|
|
|
|
|
You would need something like the following to enter 2 values.
scanf("%d %d" &i, &j);
"the debugger doesn't tell me anything because this code compiles just fine" - random QA comment
"Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst
"I don't drink any more... then again, I don't drink any less." - Mike Mullikins uncle
|
|
|
|
|
If you want to save the value into an array, you have to say which array and whereabouts in the array you want it to go. My guess is that you want something like
scanf("%d", &item[i][j]);
[Caveat Emptor: It is several decades since I wrote C programs, so my syntax may be awry]
|
|
|
|
|
While incorrect, I do not think any harm comes from extra parameters in the vararg list (they do not get popped from the stack unless a corresponding % format specifier is present). Had it been something like this instead, then it would be a problem.
scanf("%d %d", &i);
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
|
|
|
|
|
Very true, I was hoping that the person would scrutinize that line and understand that it probably didn't accomplish what they were trying to do.
"the debugger doesn't tell me anything because this code compiles just fine" - random QA comment
"Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst
"I don't drink any more... then again, I don't drink any less." - Mike Mullikins uncle
|
|
|
|
|
I have a project and this is what the project is about :
Implement a template library handling matrices.
the library must offer operation like:
• submatrix: return a matrix that contains a contiguous subrange of rows and columns of the original matrix
• transpose: return a matrix with rows and columns inverted
The matrices returned by such operators must share the data with the original matrix, but direct copy of the matrices and vectors must result in a deep copy (sharing a matrix can be obtained by taking a submatrix extending the whole row and column ranges).
Create iterators for the matrix traversing the whole data in any order.
Provide matrix addition and (row-by-column) multiplication as overloaded operators
so i did everything and i am stuck in how to implment operator overloaded for multiplication and addition, i mean should i creat a class for this operation or should i implment them in the main and how to do this.
this is the matrix interface: <pre>#include <iostream>
#include <vector>
#include <memory>
using namespace std;
template < typename T>
class matrix_interface {
public:
virtual int getCol() const = 0;
virtual int getRow() const = 0;
virtual T get(int row, int col) const = 0;
virtual T& get_ref(int row, int col) = 0;
virtual ~matrix_interface() {};
};
template < typename T>
class standard_matrix : public matrix_interface <T>{
private:
int row,col;
vector <T> data;
public:
int getRow() const override {
return row;
}
int getCol() const override {
return col;
}
standard_matrix(const vector<T> &data, int row, int col) : row(row), col(col), data(data) {
if (data.size() < row*col)
throw ("the vector is invalid");
if (row < 1 || col < 1 ){
throw ("the dimensions are invalid");
}
}
T &getRef(int r, int c) override {
if (r < 1 || r > row) {
throw "Row is off limit";
} else if (c < 1 || c > col) {
throw "Column is off limits";
} else{
T& t= data.at(col * (r - 1) + c - 1);
return t;
}
}
T get(int r, int c) const override {
if (r < 1 || r > row) {
throw "Row is off limit";
} else if (c < 1 || c > col) {
throw "Column is off limit";
} else
return data[col * (r - 1) + c - 1];
}
};
template <typename T>
class transposed_matrix : public matrix_interface <T>{
private:
shared_ptr< matrix_interface<T> > mat_ptr;
public:
transposed_matrix (shared_ptr < matrix_interface <T> > x):mat_ptr(x){
}
int getRow() const override {
return mat_ptr->getCol();
}
int getCol() const override {
return mat_ptr->getRow();
}
T get(int row, int col) const override {
if (row < 1 || row > getRow()) {
throw "Row is off limit";
} else if (col < 1 || col > getCol()) {
throw "Col is off limit";
} else
return mat_ptr->get(col, row);
}
T &getRef(int row, int col) override {
if (row < 1 || row > getRow()) {
throw "Row is off limit";
} else if (col < 1 || col > getCol()) {
throw "Col is off limit";
} else
return mat_ptr->getRef(col, row);
}
};
template <typename T>
class submatrix_interface : public matrix_interface <T>{
private:
shared_ptr< matrix_interface<T> > mat_ptr;
int rowSet, colSet, row, col;
public:
submatrix_interface(shared_ptr < matrix_interface <T> > x,
int row_start, int col_start, int row_end, int col_end)
:mat_ptr(x){
if (row_start < 1 || col_start < 1 || row_end > mat_ptr->getRow() || col_end > mat_ptr->getCol()
|| row_start > row_end || col_start > col_end)
{
throw "the input of the submatrix entered is not valid.";
}
else {
rowSet = row_start - 1;
colSet = col_start - 1;
row = row_end - row_start + 1;
col = col_end - col_start + 1;
}
}
int getRow() const override {
return row;
}
int getCol() const override {
return col;
}
T get(int row, int col) const override {
if (row < 1 || row > getRow()) {
throw "Row is off limit";
} else if (col < 1 || col > getCol()) {
throw "Column is off limit";
} else {
return mat_ptr->get(row + rowSet, col + colSet);
}
}
T &getRef(int row, int col) override {
if (row < 1 || row > getRow()) {
throw "Row is off limit";
} else if (col < 1 || col > getCol()) {
throw "Column is off limit";
} else {
return mat_ptr->getRef(row + rowSet, col + colSet);
}
}
};
this is the matrix header:
<pre>#include "iterator.h"
#include<iostream>
using namespace std;
template < typename Type>
class matrix_base;
template < typename Type >
class matrix {
protected:
shared_ptr < matrix_interface <Type> > matrixIntPtr;
public:
matrix() {}
matrix (const matrix & m){
vector < Type > v;
for (auto i = m.begin(); i != m.end() ; i++) {
v.push_back(*i);
}
matrixIntPtr = make_shared <standard_matrix <Type> > (v,m.row(),m.col());
}
matrix(const vector<Type> &data, int r, int c) {
matrixIntPtr = make_shared <standard_matrix <Type> > (data,r,c);
}
Type row() const{
return matrixIntPtr->row();
}
Type col() const{
return matrixIntPtr->col();
}
Type operator ()(int r, int c) const{
return matrixIntPtr->get(r,c);
}
Type& operator ()(int r, int c) {
return matrixIntPtr->getRef(r,c);
}
row_iter <Type> begin() const{
return row_iter <Type> (matrixIntPtr, 1, 1);
}
column_iter <Type> col_begin() const{
return column_iter <Type> (matrixIntPtr, 1, 1);
}
row_iter <Type> end() const{
return row_iter <Type> (matrixIntPtr, matrixIntPtr->row()+1 , 1);
}
column_iter <Type> col_end() const{
return column_iter <Type> (matrixIntPtr, 1 , matrixIntPtr->col()+1);
}
matrix_base <Type> submatrix(int first_row, int first_col, int last_row, int last_col) const
{
return matrix_base <Type> (make_shared <submatrix_interface <Type> >(matrixIntPtr, first_row, first_col, last_row, last_col));
}
matrix_base <Type> transpose() const {
return matrix_base <Type> (make_shared <transposed_matrix <Type> >(matrixIntPtr));
}
and this is the iterators header:
<pre>#include "matrix_interface.h"
template < typename T>
class column_iter{
private:
shared_ptr < matrix_interface <T> > sharedPtr;
unsigned row, col;
public:
column_iter(shared_ptr<matrix_interface<T> > ptr, unsigned row, unsigned col)
: sharedPtr(ptr) , row(row) , col(col) {}
column_iter& operator ++(){
row++;
if (row == sharedPtr->col() + 1) {
row = 1;
col++;
}
return *this;
}
column_iter& operator ++(int){
column_iter tmp(*this);
operator++();
return tmp;
}
const T& operator *(){
return sharedPtr->getRef(row,col);
}
bool operator == (const column_iter& x){
return col == x.col && row == x.row;
}
bool operator != (const column_iter& x){
return ! operator == (x);
}
};
template < typename T>
class row_iter{
private:
shared_ptr < matrix_interface <T> > sharedPtr;
unsigned row;
unsigned col;
public:
row_iter(shared_ptr<matrix_interface<T> > ptr, unsigned row, unsigned col)
: sharedPtr(ptr) , row(row) , col(col) {}
row_iter& operator ++(){
col++;
if (col == sharedPtr->col() + 1) {
col = 1;
row++;
}
return *this;
}
row_iter& operator ++(int){
row_iter tmp(*this);
operator++();
return tmp;
}
const T& operator *(){
return sharedPtr->getRef(row,col);
}
bool operator == (const row_iter& x){
return col == x.col && row == x.row;
}
bool operator != (const row_iter& x){
return ! operator == (x);
}
};
|
|
|
|
|
|
How to find different between current system time and user given time in c (only C not in c++) program. Is there any method that I get only time (without date) as output (not as char as time_t type).
|
|
|
|
|
You could calculate the number of seconds from midnight for current time and user given time. Subtract the two and convert back to hours, minutes seconds:
time_t now;
time (&now); struct tm *systime = localtime(&now);
struct tm user_time = {56, 34, 12}; int sys_sec = systime->tm_hour*3600 + systime->tm_min*60 + stystime->tm_sec;
int user_sec = user_time.tm_hour*3600 + user_time.tm_min*60 + user_time.tm_sec;
int diff = sys_sec - user_sec;
Of course you have to figure out what to do when time wraps around at midnight but that's your problem . I'm just showing how you can shoot yourself in the foot
Mircea
modified 31-Oct-20 14:15pm.
|
|
|
|
|
Mircea Neacsu wrote: struct tm *systime = localtime(&now); I've never understood declaring a variable as struct* .
struct is a keyword used to define a UDF. It's not a type in itself.
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
tm is a regular structure and localtime (as well as gmtime) return a pointer to an internal buffer.
Quoting from cplusplus.com:[^]
Quote: A pointer to a tm structure with its members filled with the values that correspond to the local time representation of timer.
The returned value points to an internal object whose validity or value may be altered by any subsequent call to gmtime or localtime.
Now that I look again at the code I see that I missed the call to time(&now) . Oops!
Mircea
|
|
|
|
|
It is simply a pointer to a structure that gets filled in by the system call. What is wrong with that?
|
|
|
|
|
It bothers me because it would be akin to saying:
class *Foo = new Foo(); Is that legal C++?
EDIT:
Oh wait, I didn't notice that there is a struct name followed by a variable name. I see now.
The difficult we do right away...
...the impossible takes slightly longer.
modified 31-Oct-20 16:37pm.
|
|
|
|
|
Richard Andrew x64 wrote: Is that legal C++? Yes, and necessary. Pointers must actually point to something, or they are just empty space.
|
|
|
|
|
You misunderstood what I was demonstrating. I was demonstrating declaring a variable of type "class*". Is it possible to declare a variable of type "class"? Is there such a type?
I don't think that's legal in C++. I haven't tried it though.
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
Richard Andrew x64 wrote: Is it possible to declare a variable of type "class"? No, because class is an abstract concept, not a type in the same sense that int is. The actual type will be the name used in the class definition. For example consider the following:
class Foo
{
public:
Foo() { std::cout << "Constructing a Foo object" << std::endl; }
};
int main(int argc, char** argv)
{
Foo aFoo;
Foo* pFoo = new Foo();
}
A new object aFoo is created and stored on the local stack. A second new object is instantiated on the heap and its address passed back to be stored in the pointer pFoo . The type of both objects is Foo not class .
Does that make sense?
modified 2-Nov-20 6:39am.
|
|
|
|
|
You can use the difftime() function to subtract two time_t values. If you have the time in a struct tm, you can convert it to time_t using the mktime() function. The strftime() function formats a struct tm to text format.
These APIs are all available in C90, but some options were introduced as of C99.
difftime() should incorporate leap-seconds for past dates, but I don't know if actual implementations do so.
Note that issues such as daylight-saving time, time zones, etc. are left to the programmer. Ignoring these is a common way for programmers to shoot themselves in the foot.
Freedom is the freedom to say that two plus two make four. If that is granted, all else follows.
-- 6079 Smith W.
|
|
|
|
|
Hi all,
PLease help me for Searching and displaying a record in Array of Structure in c. I think i am meesing up with pointer and array index. PLease help
struct employee{
int emp_id;
char emp_name[30];
}emp[MAX];
void findAllDetails(int *e){
printf("\n Employee Details...\n");
printf("\n Emp No : %d", emp.emp_id);
printf("\n Emp Name : %s", emp.emp_name);
}
void PrintAllDetails(int &K){
for(i=0;i<MAX;i++){
if(K==&emp[i].emp_id)
break;
if(i<MAX)
{
findAllDetails(&emp[i]);
}
}
modified 29-Oct-20 22:51pm.
|
|
|
|
|
I would have written something similar to
#include <stdio.h>
#define EMPLOYEE_ARRAY_SIZE 10
#define NAME_MAX_LENGTH 30
#define NOT_FOUND -1
struct employee
{
int id;
char name[NAME_MAX_LENGTH];
};
int find_employee_by_id( const struct employee emp_array[], int emp_array_size, int id);
void print_employee( const struct employee * pemp );
int main()
{
struct employee emp_array[EMPLOYEE_ARRAY_SIZE] =
{
{ 1, "foo"}, {2, "boo"}, {3, "goo"}, };
int id = 2;
int index = find_employee_by_id( emp_array, EMPLOYEE_ARRAY_SIZE, id );
if ( index != NOT_FOUND )
{
printf("found emplyee details:\n");
print_employee( & emp_array[index] );
}
else
{
printf("employee with id = %d not found\n", id);
}
return 0;
}
int find_employee_by_id( const struct employee emp_array[], int emp_array_size, int id)
{
int index;
for (index = 0; index < emp_array_size; ++ index)
if ( emp_array[index].id == id)
return index;
return -1;
}
void print_employee( const struct employee * pemp )
{
printf("id = %d\n", pemp->id);
printf("name = %s\n", pemp->name);
}
"In testa che avete, Signor di Ceprano?"
-- Rigoletto
|
|
|
|
|