I am trying to launch a program from another program.
Here is the code below
Figure :1
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<string.h>
int main()
{
printf("Before Execution \n");
system("c:\\Rasmi Personal\\PERSONAL\\C\\Code Block\\C_Test\\bin\\Debug\\C_Test.exe");
printf("\nAfter Execution \n");
return 0;
}
In c:\Rasmi Personal\PERSONAL\C\Code Block\C_Test\bin\Debug\C_Test project contains the code is
Figure 2:
#include <stdio.h>
int main()
{
int x = 10;
while( x --> 0 ) // x goes to 0
{
printf("%d\n", x);
} return 0;
}
But while executing the 1st program (Figure 1) the output comes as below.
Before Execution
'c:\Rasmi' is not recognized as an internal or external command,
operable program or batch file.
After Execution
Please help me in solving this.
PS:- I am using CODE::BLOCKS in Windows XP.
You're using path names with spaces in them. Everything gets more confusing when you do that, and you have to add quotes around the right things in the right places to get anything to work.
I recommend using path names without spaces in them.
If you still want to try to make this work with spaces in your path names, the following might do it:
system("\"c:\\Rasmi Personal\\PERSONAL\\C\\Code Block\\C_Test\\bin\\Debug\\C_Test.exe\"");
Related
Hello and thank all who bothered to read. I am currently trying to execute a program in c that contains a function with this for loop.
#include <stdio.h>
#include <string.h>
int main(int argc, char **argv){
int i, goal;
char resp[1];
printf("How many people would you like to ask?");
scanf("%d" ,&goal);
for(i = 0; i < goal; i++){
printf("-Person %d: Would you like to register to vote?\n",(i+1));
scanf("%s", resp);
if((strcmp(resp, "n"))==0){
printf("\nOk.\n");
}
else if((strcmp(resp, "y"))==0){
printf("\nRegistering...\n");
}
}
printf("\n%d people asked! Taking a break.\n", goal);
}
Yet for some reason, when I run the program, after pressing n for all of the runs, the for loop does not increment, leading to an infinite loop on the final run.
I have tried changing the for loop to a while loop with the same results and have also tried the y option for the last time which led to a whole host of other issues. In order to get the goal value I am using scanf though when I tried using fgets I still came into the same flawed result or worse. I ran this code into a debugger and when executing the program line by line it works fine, but running it normally seems to be the issue. Any help is appreciated and I am willing to clarify or expand on my code. This is my first post here so apologies if this has been asked 50 trillion times.
I'm new to the programming world, recently I began my programming path with C, and because of that I made a program that determines if a number is whether perfect or not. I use Code::Blocks IDE, and it works just fine, the problem is when I click the option "Build and run", the IDE executes the program and works perfectly, but when I select the .exe file from my desktop, it opens up, but doesn't show any output, the window just closes suddenly. Does someone have any idea on how to solve this issue?
Code:
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
int main() {
int N;
int j;
int Sum = 0;
printf("Input a number.\n");
scanf("%d",&N);
for (j = 1; j < N; j++)
{
if (N%j==0)
{
Sum+=j;
}
}
if (Sum==N)
{
printf("The number is perfect.\n");
} else {
printf("The number is not perfect.\n");
}
return 0;
}
Running program with Code::Blocks Build and run option
The only part of the Desktop located .exe that I can reach
If someone can suggest a solution, I will be very thankful!
As it was said, the program exits immediatly after it's completion. If you want to run the program, executing it by double click, you can place a pause condition in the program, for instance if you put getchar(); just before the return 0; statement, it will only exit after it receives an input from the keyboard, that is after you enter a key.
I'm following along the Head First C (2012) and am running into an issue with their initial "cards.c" project in the first chapter.
Basically, the code asks for an input for a type of card then outputs a value based on whatever the user entered. However, I find that the command prompt exits automatically after executing the corresponding if statement even if I tell the program to pause or check for a key input at the end of the main() function.
I've made a half-fix by including the output of the card's value within the if statement such as
if (card_name[0] == 'K') {
val = 10;
printf("The card value is: %i\n", val);
system("pause");
}
Yet I find the output will occur twice if I leave the printf statement and system pause both within the for bracket and at the end of main. As such, I'm confused why the command prompt is automatically closing when I only output at the end of main rather than inside of each if/else if/else statement.
For reference, I am running through Visual Studio Code on Windows 10 and am using MinGW to compile (Which VSCode allows me to build with, but I run through a command prompt).
#include <stdio.h>
#include <stdlib.h>
int main()
{
char card_name[3];
puts("Enter the card name: ");
scanf("%2s", card_name);
int val = 0;
if (card_name[0] == 'K') {
val = 10;
} else if(card_name[0] == 'Q') {
val = 10;
} else if(card_name[0] == 'J') {
val = 10;
} else if(card_name[0] == 'A') {
val = 11;
} else {
val = atoi(card_name);
}
printf("The card value is: %i\n", val);
system("pause");
return 0;
}
For anyone reading, this is exactly how the code appears in the book outside of the system pause at the end of main(). I apologize if this is too basic of a question, but I have yet to find an answer after an hour of searching on this site.
I was able to get it working using the advice provided by paddy to use fgets instead of just printing as my character was being eaten by the buffer while passing. I also launched directly through the command line as my setup for VSCode wasnt done properly and was running into issues itself trying to run the compiled exe.
This C code is supposed to create some random numbers and print them and then sort them and print them again, but it just prints the sorted numbers. Could any body help me?
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
int main(){
int i, j, k;
float temper;
time_t t;
float grades[1000];
fflush(stdout);
printf("Here are the number\n");
srand(time(&t));
for(i=0;i<1000;i++){
grades[i]=rand();
printf("%f\n", grades[i]);
}
for(i=0;i<1000;i++){
int swap=0;
for(j=i;j<1000;j++){
if(grades[i]>grades[j]){
temper=grades[i];
grades[i]=grades[j];
grades[j]=temper;
swap=1;
}
}
}
printf("sorting is done");
for(i=0;i<1000;i++){
printf("%f\n", grades[i]);
} }
Your program is working correctly. Try changing everything from 1000 to 10 just to test and see for yourself.
What is happening is that it is printing everything out so quickly that the first 1000 is off the page.
The code is correct.
Try a small size to array,
write all the logs to a file.
Code worked fine for me too. Maybe your terminal is not storing enough lines for you to see the beginning of the output. You can change that in the settings for your terminal. Or you can cat them to a file instead. There is an easy option to do so if you google it. Also, add in another printf in between as a marker that is obvious like:
printf("+++++++++++++++++++++++++++ here is the break point ++++++++++++++++");
It will make it that much harder to miss it. Good luck!
PS: to cat your output to a file simply type '> filename' when running the program. I called mine math.c so when I ran I typed:
'$./math > file'
And the whole output in in a file named 'file'
#include <stdio.h>
#include <cstdlib>
rec();
main()
{
int a, fact;
char q, n, y;
printf("\nEnter any number ");
scanf("%d", & a);
fact = rec(a);
printf("Factorial value = %d\n", fact);
printf("do you want to exit.....(y/n):");
scanf("%s" ,&q);
if (q == 'n')
{
system("cls");
main();
}
else
return 0;
}
rec(int x)
{
int f;
if (x == 1)
return 1;
else
f = x * rec(x - 1);
return f;
}
I'm using code blocks but I don't know how to clear the screen. I searched then found system("cls"); within header file #include<cstdlib>, but it shows the error cstdlib: no such file of directory. What should I do ?
Change
#include <cstdlib>
to
#include <stdlib.h>
cstdlib is a C++ header file, and thus will be unusable in C.
Clearing the screen is outside the purview of a normal C program. It depends on the operating system.
For windows, you should look into conio.
For unix, look into curses or termios.
system() always launches a sub-shell which may or may not have any effect on the environment of the parent program. You do need a system-call, but not a system() call.
I didn't always know this. I once (long ago) suggested in comp.lang.c that someone should try system("exit"); to close the window around the DOS program. But that, of course, cannot work. And I was quickly advised to test my code before posting. :)
you have lots of problems in your code....
but for the specific problem, try #include <stdlib.h>
use the #include<stdlib.h> that's where the clear screen function is defined.
To use system("cls") you need the header <iostream>. This will allow all system() types to execute. Unsure if it is a C++ header file, but it works for the compiler that I use.