|
Tarun Jha wrote: ...the compiler is showing logic error. Highly unlikely. If anything, it is showing you syntax errors (e.g., endless loop).
"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
|
|
|
|
|
OP doesn't understand what is happening. The code compiles OK, it's the execution that fails.
|
|
|
|
|
How to sort digits of a number?. Can we use bubble sort? If so, how to use values inside for loop?
|
|
|
|
|
Quote: How to sort digits of a number? There are several way to do that. A simple approach could be convert the number to a string and then sort the string characters.
Quote: Can we use bubble sort? Of course. Anyway, you know, bubble sort is inefficient. However that strongly depends on the size of the input (in your case, I assume the size of the input very small).
Quote: If so, how to use values inside for loop?
Quote Selected Text What do you mean?
|
|
|
|
|
|
I am writing a C program to convert input value to hours minutes.
eg :
input : 126, output : 2 : 6, input : 45 , output : 0 : 45.
For values like 3663, its printing
61 : 3
1 : 1
instead of 61 : 3.
My logic is :
int t1,t2,r=0,n,t;
printf("Enter time\n");
scanf("%d",&t1);
while(t1>0){
printf("%d\n",t1);
if(t1<60){
break;
}
t=t1%60;
r=r+t;
t1=t1/60;
printf("%d : %d\n",t1,t);
}
What is wrong here?
|
|
|
|
|
It is your while loop that checks t1 which gets the number of hours assigned in the loop. With input 3663, t1 is set to 63 within the first loop iteration which is then processed and shown as "1:1".
Add the missing scanf() call at the end of the loop to read the next user input and remove the unnecessary lines:
scanf("%d", &t1);
while (t1 > 0){
printf("%d\n", t1);
t = t1 % 60;
t1 = t1 / 60;
printf("%d : %d\n", t1, t);
scanf("%d", &t1);
}
|
|
|
|
|
Indent your code properly, and use meaningful names for your variables and the problem becomes clearer:
int minutes = t1 % 60; int hours = t1 / 60;
You could add a third calculation to convert the hours to days and hours.
|
|
|
|
|
Hello friends,
I am not able to understand this,could you help me.I am using Qt creature
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
void S7API CliCompletion(void *usrPtr, int opCode, int opResult);
~MainWindow();
.
.
.
void MainWindow::CliCompletion(void *usrPtr, int opCode, int opResult)
{
}
|
|
|
|
|
Such is often used to define a build specific calling convention (Calling convention - Wikipedia[^]).
In your case it seems to be defined in snap7.h[^]:
#ifdef OS_WINDOWS
# define S7API __stdcall
#else
# define S7API
#endif
I don't know how it got there. It is not Qt related and the QtCreator is probably ignoring it. It looks like you have copied the function definition from somewhere into your header file.
|
|
|
|
|
I added .h .c and .lib in my project so,so that it is defined in qt
|
|
|
|
|
Then there is no need to re-define and re-implement the function when already located in a library or an additional source file which has been added to the project.
However, I think pointing to calling conventions should have answered your question.
|
|
|
|
|
The function that I indicate above is related the example that it is client .I did just copy past properly.I wrote about calling convention and it say me ,you have to use macro with in .h and .c (prototype and function).Now, Qt didnt add the macro with function .I suppose I have to add .Am I right
|
|
|
|
|
Don't add the macro to a Qt class function.
You have two choices:
- Use the existing header and source files from the examples and add them to your project. Then call the function using the name and include the header file in the source files where you call it.
- Convert the examples by implementing all the code in Qt classes (source and header).
I would go for the first option. Than there is no need to implement a similar function as Qt class member. You might do so, but then the Qt function must not use the macro:
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
void CliCompletion(void *usrPtr, int opCode, int opResult);
};
#include "snap7.h"
#include "MainWindow.h"
void MainWindow::CliCompletion(void *usrPtr, int opCode, int opResult)
{
::CliCompletion(usrPtr, opCode, opResult);
}
|
|
|
|
|
So,You mean ,I must not use the macro because of the reason that you say.I added in my project .lib,.h,.c and Qt is handling this instead of me.I understand like this,I am new communication engineer this is why I dont know perfect C++.
By the way I create a new function Clicompletion is the function that I create,I will use it any where.
Maybe I got it wrong you.
|
|
|
|
|
You must not use the macro with Qt classes (and should not use it with C++ classes).
If you have another source file (or library) where the function is implemented, there is no need to implement it again as MainWindow class member.
It is even not necessary to use the macro when the function is part of a C++ source file in your project. It is only necessary when using a C library. Then the header file tells the compiler that the function in the library uses that calling convention and that it must create corresponding code when creating the assembly code for the function call.
C and C++ might use different calling conventions (depends also on the used compiler). Therefore, a compiler must know the calling convention when a C function is called from C++ code.
|
|
|
|
|
#ifdef OS_WINDOWS
# define S7API __stdcall
#else
# define S7API
#endif
Because of I use windows C++,I dont have to this with my functions,right?
|
|
|
|
|
It depends. See also my above post.
Such constructs are not necessary for C++. But they are required when calling C functions from C++ code depending on how the C code has been build. If the C code is part of your project and you set the project options to compile C code as C++ (that is: use the C++ compiler also for C code), there is no need for the macro. But if the C code has been compiled as C for Windows, it must be used.
|
|
|
|
|
As a result of these informations, as far as I understand,I can call the functions just with name that intellisense show me functions,so that no problem with snap7 and I can create functions as I always do.This would be strange with macro.
|
|
|
|
|
This is not a C language issue, it is something to do with Qt. S7API is a define variable that will be in one of the Qt header files, and perhaps also in the documentation.
|
|
|
|
|
|
This is my logic to find the longest word in C.
void find_long(char *a)
{
int l,i,l1=0,j=0,l2=0;
char b[50],c[50];
l=strlen(a);
for(i=0;i<l;i++){
if(a[i]!=" " ){
="" b[j]="a[i];
" printf("%c="" ",b[j]);
="" j++;}
="" else="" if(a[i]="='" '){
="" ;
="" if(strlen(b)="">l1){
strcpy(c,b);
l1=strlen(b);
j=0;
}
else{
printf("\nEOF");}
}
}
printf("\nlongest is \n");
printf(c);
But it is not taking the last word I am giving. What is wrong here?
|
|
|
|
|
You only check the length of each word when you encounter a space character. So if the last character is not a space then when the loop ends you still have an unchecked word in array b. Your code would be clearer if you formatted it properly and used <pre> tags round it. And also used meaningful names for your variables rather than single letters.
|
|
|
|
|
what condition I can add here to avoid it?
|
|
|
|
|
At the end of the loop you just need to check the last word in array b.
|
|
|
|