How to make a prompt in C [closed] - c

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
New programmer here, I have a few lines of code that I've finally managed to get to function but I'm running into a bit of a design issue. The code itself, at least to me, function as intended but I don't know how to get my terminal to display "Insert number here". This is how to code currently looks.
#include "cs50.h"
#include <stdio.h>
int main(void)
{
int n;
do
{
n = GetInt();
printf("Your number is!"/n);
}
while (n<1)
{
return n;
}
}
I'm looking for a way to get the same outcome as n = get_int ("Insert number: "); but that method brings up errors such as:
prompt.c:10:7: warning: implicit declaration of function 'get_int' is
invalid in C99 [-Wimplicit-function-declaration]
{ n = get_int("Insert number here %i\n", n);
or if I'm using n = GetInt("Insert number: "); I get this error message.
prompt.c:10:14: error: too many arguments to function call, expected 0,
have 2
{ n = GetInt("Insert number here %i\n", n);
~~~~~~ ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/local/include/cs50.h:87:1: note: 'GetInt' declared here
int GetInt(void);
^
1 error generated.
Is there a way to get this to function so my terminal can prompt me with "Insert number here: ". Thank you and cheers!

If you want to print "Insert Number here:", you may use the prinf function.
#include <stdio.h>
int main()
{
int testInteger;
printf("Insert Number here: ");
scanf("%d", &testInteger);
printf("Your Number is %d",testInteger);
return 0;
}

Related

My code is not woriking. am I doing it wrong? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I just have studied programming and english. I'm a newbie.
My code is supposed to show a sum of 1 to entered number.
(ex : if you enter 5, answer is 15 and if you enter 10, answer is 55)
Bu this code is not working. I have tried many times to fix it, but i don't know why this code is not working.
#include <stdio.h>
int main(void){
int i = 0;
int sum;
int j;
scanf("%d\n", sum);
for(j = 1; j <= sum; j++){
i = i + j;}
printf("%d\n", i);
return 0;
}
The problem with your code is a simple one, and a decent compiler will warn you about it:
testprog.c: In function ‘main’:
testprog.c:9:11: warning: format ‘%d’ expects argument of type ‘int *’,
but argument 2 has type ‘int’ [-Wformat=]
scanf("%d\n", sum);
^
The scanf function requires the address of items you want populated since it needs to change them. Passing the actual item (since C is pass-by-value) to a function could only ever change the copy rather than the original.
You can see the correct way to do it in the standard:
d: Matches an optionally signed decimal integer, whose format is the same as expected for the subject sequence of the strtol function with the value 10 for the base argument. The corresponding argument shall be a pointer to signed integer.
As an aside, I've fixed that issue and added some more improvements, such as:
less confusing variable names (at no stage is your sum variable a sum of anything);
better (more logical) layout of code;
better scoping of temporary variables like i.
prompting the user for input so that they know what they're supposed to enter; and
checking for valid input by checking return value of scanf.
The code is:
#include <stdio.h>
int main(void){
int maxNum;
int sum = 0;
printf("Enter number to sum up to: ");
if (scanf("%d", &maxNum) != 1) {
fprintf(stderr, "Problem getting input\n");
return 1;
}
for (int i = 1; i <= maxNum; ++i) {
sum += i;
}
printf("Sum is %d\n", sum);
return 0;
}
Try this :
#include <stdio.h>
int main(void){
int i = 0;
int sum;
int j;
scanf("%d", &sum);
for(j = 1; j <= sum; j++){
i = i + j;}
printf("%d\n", i);
return 0;
}
Your mistake is just wrote scanf("%d\n", sum) instead of scanf("%d", &sum);
c language supports a type of call by value what transfers data copied.
So, you need to give the memory address that scanf function can write user's input to the sum variable memory.
scanf("%d", &sum)

how do i store a in an array in C? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I'm writing a program that calculates results of football matches.I'm trying to store the team ids' in an array with a length of 10 defined at the top but i keep getting getting a build error from the array. I realise the syntax might be wrong but how else can i use a variable to specify array length?
The error message i'm getting is : expected expression before '{ ' token.
#include <stdio.h>
#include <stdlib.h>
#define ARRAY_SIZE 10
int main() {
int numberofmatches, hometeamid, awayteamid, hometeamgoals, awayteamgoals;
int hometeamwins = 0;
int winratio;
int teamid[ARRAY_SIZE];
printf("Enter number of matches played \n");
scanf("%d", &numberofmatches);
if (numberofmatches > 0) {
int x = 0;
do {
printf("Enter match stats in order Home_team_ID,Away_Team_ID,Goals_Home,Goals_Away\n");
scanf("%d %d %d %d", &hometeamid, &awayteamid, &hometeamgoals, &awayteamgoals);
teamid[ARRAY_SIZE] = {hometeamid}; //Error is on this line
if (hometeamgoals > awayteamgoals) {
hometeamwins++;
}
x++;
}
while (x < numberofmatches);
winratio = hometeamwins / numberofmatches;
printf(" %d :teamidth %d :winratio", teamid[0], winratio);
}
return 0;
}
This
teamid[ARRAY_SIZE] = {hometeamid};
is the syntax for defining an array of size ARRAYSIZE and initialising it incompletely.
You try it in the middle of a loop.
In case you want to write to an array member you probably want
teamid[x] = hometeamid;
Also, I recommend making sure that you do not write beyond teamid[9], which is the last legal member of the array, for ARRAY_SIZE == 10.
Your defining an array of size 10, and then accessing it at index 10. Since arrays are indexed 0..n-1 you need to access index 9 to get the end of the array instead of 10.

How do you get C to produce output from number inputted in code? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
What code do you have to include to get C to produce an output from number inputted into the system.
#include <stdio.h>
int main()
{
int z;
printf("Please enter a number");
return 0;
}
I want the programme to work out a mathematical equation to the number that the user enters, for example if I enter 5 I want it to work out the exponent 2 of 1 up to that integer input.
You can use scanf function to get input from user like this:
#include <stdio.h>
int main()
{
int z;
printf("Please enter a number");
scanf("%d", &z);
// do your calculations on z
printf("Result: %d", z);
return 0;
}
You can use the scanf function.
For a number (or in your case an integer specifically) the line is
scanf("%d", &z);
where the %d specifies that its reading in an integer and the &z is a pointer to your integer variable z.
Information here: https://www.tutorialspoint.com/c_standard_library/c_function_scanf.htm
I'm guessing you're new to C programming, so once you get the basics down I would suggest familiarizing yourself with pointers as they are a critical aspect of the language. Some useful resource here using google-fu : https://www.tutorialspoint.com/cprogramming/c_pointers.htm

What's wrong with my first 'c' bubble sort programme? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I just want to know what is the wrong with it, and how to fix it.
It looks like every thing is right, but when you run the .exe it crash every time
#include <stdio.h>
#include <stdlib.h>
// first list filling function:
void T_filling(int T[],int n){
int i;
for(i=1 ;i<=n ;i++){
printf("enter the number:",i+1);
scanf("%d",&T[i]);
}
}
//then the main algorithm:
int main()
{
int j,k,l;
int n,x;
// you can order up to 100 integer number
int T[100];
printf("This program is to order numbers decreasingly\n");
printf("how many numbers you want to order?\n");
// scanning the number of elements in the list
scanf(n);
//filling the list
T_filling(T[100],n);
//bubble sort Algorithm
for(j=1;j<=n-1;j++){
for(k=1;k<=n-j;k++){
if(T[k+1]>T[k]){
x=T[k];
T[k]=T[k+1];
T[k+1]=x;
}
}
}
for(l=1;l<=n;l++){
//printing the result on screen
printf("%d;",T[l]);
}
printf("\n");
system("pause");
return 0;
}
You are not using scanf and passing the array to function properly.
Please modify these lines in your code as follows and your program will work as expected.
scanf("%d", &n);
//filling the list
while(n > 100)
{
printf("Exceeding size, please re enter the size");
scanf("%d", &n);
}
T_filling(T,n);
Hope this helps.

what is wrong with my factorial program ??? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
the output numbers are wrong why ?
the program is used to get factorial of a number using recursion
and if you know sites to practice more examples I will be thankful for you
#include <stdio.h>
#include <stdlib.h>
int factorial(int a);
int main()
{
int n,x;
printf("enter ur number ");
scanf("%d",&x);
n=factorial(x);
printf("the factorial = %d",n);
return 0;
}
int factorial(int a)
{
int fac;
if(a<=1)
{
if (a<1)
{
fac=0;
}
return fac;
}
printf("the number = %d\n",a);
printf("the factorial = %d\n",fac);
fac = a * factorial(a-1);
This will always return 0, because the last fac will always be 0 and it multiplies all the other results. Change that to
if(a<1)
fac=1;
Also indent your code properly.

Resources