Printing several \t in one line - c

For some weird reason my little program over here doesn't show up the way I want to.The last \t before Quit for a weird reason doesn't do anything.
Can anyone explain me why?
#include <stdio.h>
int n,litera;
void main (void)
{ n=1;
printf("File\tEdit\tView\tCompile\tQuit\n");
printf("Selectati optiunea aleasa:");
}

There's nothing really weird about it. Since Compile is more than 4 letters long, it eats up three spaces into the next tab block. Hence the result. Use two \t\t otherwise.
Here's a Q&D solution:
#include <stdio.h>
#define TAB " "
int n,litera;
int main (void)
{ n=1;
printf("File"TAB"Edit"TAB"View"TAB"Compile"TAB"Quit\n");
printf("Selectati optiunea aleasa:");
return 0;
}

Related

debugg not going inside the loop

I am practicing on chars and i wrote a while loop that ends whenver i see a ";"
however when i am trying to debug it doesnt go inside the loop only at the last loop and I dont understand why
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void main()
{
char str[] = "abccc;";
int i = 0;
while (str[i] != ';')
{
i++;
printf("%d \n",i);
}
}
thats the code, it works and realy simple but if I debugg to see it only goes inside the loop in the last time
I am not sure why and how to fix
I would suggest keep tracing even if it looks like its not going inside the loop. Sometimes the way the code is generated, the execution in the debugger is not linear. In fact as you keep tracing, keep a watch for output of the printf from the program.

for loop in c is not incrementing on the final iteration of the loop

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.

C - How to use a string with scanf multiple times

Here's my code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void)
{
char terminal[100];
printf("Enter cmds: ");
scanf(" %s", terminal);
if(strcmp(terminal, "help") == 0){
printf("test");
scanf(" %s", terminal); // trying to detect if a user types
// "help" the menu will pop up again
}
return 0;
}
When a user types "help", the menu pops up, (good so far). But when they type "help" again, the menu does not pop up. Does anybody know what is going on?
The initial comments hit the nail on the head here. You need to loop over new input multiple times. This can be done fairly easily.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void)
{
char terminal[100];
printf("Enter cmds: ");
// this expression will return zero on invalid input, exiting the loop
while (scanf("%99s", terminal) == 1) {
// your if statement and other code you want to repeat go here.
}
}
To better encapsulate this kind of behaviour, defining some sort of function that compares strings and returns an element of an enum is a very common practice, but not required in this question.

printf doesn't work

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'

Easy C: Program stuck after asking for prompt

In the following simple program after the user inputs an integer the command line remains a flashing prompt. When I exit the program the answer is then printed out. Why is this and how can I fix it?
//powers of 2
#include <stdio.h>
int main(void){
int a,b=1,i;
printf("What power of 2?\n");
scanf("%i\n",&a);
for (i=0; i<a;i++)
{
b=b*2;
}
printf("the answer is: %i\n",b);
return 0;
}
Try remove the \n in your scan :
scanf("%i",&a);
Remove \n from scanf. After that I compiled your program and it worked properly.

Resources