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
#include <string.h>
#include <conio.h>
#include <math.h>
#include "helper2.h"
char validLoop = ' ';
int choice;
int validInput = 0;
main(){
repeat:
clrscr();
printf("=======================\nMenu\n=======================\n[1] Binary to Decimal\n[2] Sorting Algorithm(Ascending Descending)\n[3] Palindrome Checker\n=======================\n");
do{
printf("Enter Your Choice: ");
scanf("%d", &choice);
if (choice == 1 || choice == 2 || choice == 3){
validInput = 1;
}
else{
printf("Invalid Input! Please Input a value given within the choices.\n");
}
} while(validInput == 0);
if (choice == 1){
BinToDec();
}
if (choice == 2){
sorting();
}
if (choice == 3){
checker();
}
printf("\nPress Y||y to repeat or any key to exit.");
validLoop = getche();
if (validLoop == 'y' || validLoop == 'Y'){
goto repeat;
}
else{
return 0;
}
}
im getting errors on where my validLoop, choice, and validInput isnt being initialized and says "Declaration is not allowed here." it also says a statement is missing a ; on line 11. am i doing something wrong in my part?
edit: sorry, i forgot to add the header:
char checker(){
int i;
int check = 0;
char checker();
char word[50];
printf("Insert a string: ");
scanf("%s", &word);
for (i=0;i<strlen(word)/2;i++){
if (word[i] == word[strlen(word)-i-1]){
check++;
}
}
if (check == strlen(word)/2){
printf("%s is a palindrome", word);
}
else{
printf("%s is not a palindrome", word);
}
return 0;
}
int BinToDec(){
char binary[8];
int i;
int sum=0;
int value;
int length;
printf("Input binary number you want to convert to decimal: ");
scanf("%s", binary);
length = strlen(binary);
for (i=strlen(binary)-1;i>=0;i--){
value = length - (i + 1);
if ((int)binary[i]-48 == 1){
sum = sum + pow(2.0, (float)value);
}
}
printf("%d", sum);
getch();
return 0;
}
int sorting(){
int i;
int sort[10];
int min;
int temp;
int currentElement;
int compareElement;
printf("Input integers to sort: ");
for (i=0;i<10;i++){
scanf("%d", &sort[i]);
for (currentElement=0;currentElement<(sizeof(sort)/sizeof(sort[0]))-1;currentElement++){
min = currentElement;
for (compareElement=currentElement+1;compareElement<(sizeof(sort)/sizeof(sort[0]))-1;compareElement++){
if (sort[compareElement] < sort[currentElement]){
min = compareElement;
}
}
temp = sort[currentElement];
sort[currentElement] = sort[min];
sort[min] = temp;
}
printf("Ascending Order: ");
for (currentElement=0;currentElement<=(sizeof(sort)/sizeof(sort[0]))-1;currentElement++){
printf("%d, ", sort[currentElement]);
}
printf("\n");
printf("Descending Order: ");
for (currentElement=(sizeof(sort)/sizeof(sort[0]))-1;currentElement>=0;currentElement--){
printf("%d, ", sort[currentElement]);
}
printf("\n");
return 0;
}
basically the whole this is supposed to be a program to call my functions from the header file. everything was smooth sailing and it was even working before i tried adding validations on my part. i dont know if this really helps but i would appreciate every feedback with this. im using a turbo c emulator as my compiler and working environment.
printf("Input integers to sort: ");
for (i=0;i<10;i++){
scanf("%d", &sort[i]);
Seems closing } is missing. Writing actual definitions in header is not a good habit.
Other coding issues aside.
In the code that you show is in your header file you have the following line:
char checker();
within function char checker() I suspect that this is your culprit.
Related
I was trying to make a simple function to make a group of number that user enters them, using pointer of pointer but i keep getting this error and its hard to tell where the problem is, if there any other option to use something else that tells me where the problem is instead of this weird error.
#include <stdio.h>
void BuildGroub(int** group,int* count){
int i=0;
int j;
printf("Enter the size of the group \n");
scanf("%d", &*count);
while(*count != 0){
printf("Enter the %d number of the group:\n", i);
j=0;
scanf("%d", &**(group+i));
while(**(group+i)!=**(group+j)){
j++;
}
if(j==i){
i++;
count--;
} else{
printf("you have already entered this number please try again: \n");
}
}
}
int main(){
int count;
int group[100];
int *groupptr = &group;
BuildGroub(&groupptr,&count);
for(int i=0;i<count;i++){
printf("%d, ", group[i]);
}
return 0;
}
With this question, you do not need to use double pointer. If you want to learn how to use the double pointer, you can google then there are a ton of examples for you, for example, Double Pointer (Pointer to Pointer) in C.
In BuildGroub you decrease the count pointer
if(j==i){
i++;
count--;
}
, but in the condition of while loop, you compare the value that count pointer points to. it seems strange.
while(*count != 0)
Even if you change count-- to (*count)--, it will decrease the number of elements that you enter to 0 when you get out of the while loop, then in main function:
for(int i=0;i<count;i++){} // count is equal to 0 after calling BuildGroub function if you use (*count--) in while loop.
You should use a temp value for while loop function, for example:
int size = *count;
while(size != 0){
...
if (i == j) {
i++;
size--;
}
}
You should use, for example, group[i] instead of *(group+i). It will be easier to read your code.
The code complete:
#include <stdio.h>
void BuildGroub(int* group,int* count){
int i=0;
int j;
printf("Enter the size of the group \n");
scanf("%d", count);
int size = *count;
while(size != 0){
printf("Enter the %d_th number of the group:\n", i);
j=0;
scanf("%d", &group[i]);
while(group[i] != group[j]) {
j++;
}
if(j==i){
i++;
size--;
} else{
printf("you have already entered this number please try again: \n");
}
}
}
int main(){
int count;
int group[100];
int *groupptr = group;
BuildGroub(groupptr,&count);
for(int i=0;i<count;i++){
printf("%d, ", group[i]);
}
return 0;
}
The test:
./test
Enter the size of the group
5
Enter the 0_th number of the group:
1
Enter the 1_th number of the group:
2
Enter the 2_th number of the group:
2
you have already entered this number please try again:
Enter the 2_th number of the group:
3
Enter the 3_th number of the group:
3
you have already entered this number please try again:
Enter the 3_th number of the group:
4
Enter the 4_th number of the group:
5
1, 2, 3, 4, 5,
If you want to use a double pointer, you need to change your function like this:
void BuildGroub(int** group, int* count) {
int i = 0;
int j;
printf("Enter the size of the group \n");
scanf("%d", &*count); //I think this is redundant but works.
while (*count != 0) {
printf("Enter the %d number of the group:\n", i);
j = 0;
scanf("%d", (*group + i)); //The content of group + i
while ( *( *group + i) != *(*group + j)) { //the content of the content
j++;
}
if (j == i) {
i++;
(*count)--; //The content decrement
} else {
printf("you have already entered this number please try again: \n");
}
}
}
But you have a big problem in main and it is because you are using the parameter count to decrement until zero inside the function. So when the function finish, count value is zero and you don't print anything... You need to change this, using a internal variable to make the count, and finaly, setting the parameter to be using in main.
I want to use a function to scanf up to 10 values for an array with the size 10, and also keep track of the number of values that are in the array because I'll need it later for solving some maths about the array, (max value, min value, etc.).
#include <stdio.h>
int enter(int MeasurmentData[], int nrOfmeasurments)
{
for(int i=0;i<10;++i)
{
int MeasurmentData[10];
scanf("%d",&MeasurmentData[i]);
int nrOfmeasurments = 0;
nrOfmeasurments ++;
return nrOfmeasurments;
}
int main()
{
int MeasurmentData[10];
int nrOfmeasurments;
char menuoption;
while (1)
{
printf("Measurment tool 2.0\n");
printf("v (View)\n");
printf("e (Enter)\n");
printf("c (Compute)\n");
printf("r (Reset)\n");
printf("q (Quit)\n");
printf("enter your option:\n");
scanf(" %c", &menuoption);
if (menuoption =='e') \\ enter values
{
int MeasurmentData[10];
int nrOfmeasurments;
enter(MeasurmentData, nrOfmeasurments);
}
else if(menuoption == 'v') \\\ view values
{
//printf("%d", MeasurmentData[]);
}
else if(menuoption == 'c')
{
}
if(menuoption == 'q')
{
printf("Exiting Measurment tool 2.0\n");
return 0;
}
}
}
When I run the program it should print Measurment tool 2.0, after the the user has the choice of inputting e(enter) which will scan in up to 10 values into an array, if the user clicks q(quit) while in the enter option already he will be returned to the main menu where he can do whatever.
V(view) prints out the array for the user so that he can view what elements are inside.
C(compute) uses the elements inside and the nr of elements to calculate the highest value element, lowest.
There are some errors in your code. Ill try to explain. You have over declared your variables too many times. And since you have a fixed loop you don't need to count the measurements you will always read 10 measurements.
Below are the code with some modifications. Feel free to ask anything about it.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAXIMUM_MEASURMENT 10
int enter(int MeasurmentData[])
{
char input[100];
int nrMeasurement = 0;
// reseting Measurment data
for(int i=0;i<MAXIMUM_MEASURMENT;++i) MeasurmentData[i] = 0;
for(int i=0;i<MAXIMUM_MEASURMENT;++i)
{
scanf("%99s", input);
if(strcmp(input, "q") == 0) {
break;
}
MeasurmentData[i] = (int) strtol(input, (char **)NULL, 10);
nrMeasurement++;
}
return nrMeasurement;
}
void showMeasurments(int* MeasurmentData, int length) {
int i = 0;
printf(" ======== Measurment ======== \n");
for(i = 0; i < length; i++) {
printf("%d ", MeasurmentData[i]);
}
printf("\n");
}
int main()
{
int MeasurmentData[MAXIMUM_MEASURMENT];
int nrOfmeasurments;
char menuoption;
while (1)
{
printf("Measurment tool 2.0\n" "v (View)\n" "e (Enter)\n" "c (Compute)\n" "r (Reset)\n" "q (Quit)\n enter your option:\n");
scanf(" %c", &menuoption);
if (menuoption =='e') // enter values
{
enter(MeasurmentData);
}
else if(menuoption == 'v') // view values
{
// show
showMeasurments(MeasurmentData, MAXIMUM_MEASURMENT);
}
else if(menuoption == 'c')
{
}
if(menuoption == 'q')
{
printf("Exiting Measurment tool 2.0\n");
break;
}
}
return 0;
}
Edit: i have updated the code. So i have read the comments of your question and there you have explained a little better what you are trying to accomplish. So since you have the requirement to press 'q' to stop reading values. I have to read all measurments as string and convert to integer if it is not the character q.
Edit 2: Thanks to #user3629249 to point out some of the flaws from the code ill update with his suggestions.
I made this simple program that asks the user to input the number of columns the matrix called arp is going to have because, that way when the program asks the user to input a number so it can find matches on the numbers stored at the array without comparing all 10 columns allocated on memory with array to pointer type.
The problem here comes when the user inputs into the columns size definition 2. All works fine before the last function of p3() does its job, then it doesn't even return to main to execute the infinite loop defined there. I have tried removing the pointers and didn't work; I also tried removing other parts of the code but still nothing...
Update: Tried removing the function to find elements felmnt() and still the same.
Here is The Buggy Code:
#include <stdio.h>
#include <stdlib.h>
int loop = 1;
void keepalive(void)
{
int ckr = 0;
fflush(stdin);
printf("\n\n ******[s]<< CONTINUE | EXIT >>[n]******\n");
while(printf(" > ") && (ckr = getchar()) != 's' && ckr != 'n') fflush(stdin);
getchar();
if(ckr == 'n') loop = 0;
system("CLS");
}
void felmnt(int *colu, int (*arp)[10])
{
int nius=0, colmts, x, i, ejct;
do
{ // loop to let the user find more elements inside matrix
colmts=0;
printf("\n Insert The Number To Find\n > ");
scanf("%d", &nius);
for(x=0; x<*colu; x++) // search of element inside matrix
{
for(i=0; i<=9; i++)
if(nius == arp[i][x])
colmts++;
}
if(colmts>1) // results printing
{
printf("\n %d Elements Found", colmts);
}else if(colmts)
{
printf("\n 1 Element Found");
}else
{
printf("\n Not Found");
}
printf("\n TRY AGAIN? s/n\n > ");
ejct=getchar();
getchar();
}while(ejct=='s');
}
void mat(int *colu, int (*arp)[10])
{
int ci, cn, tst=0;
for(ci=0; ci<*colu; ci++)
{
for(cn=0; cn<10; cn++)
{
printf("\n Input The Number [%d][%d]\n > ", ci+1, cn+1);
scanf(" %d", &arp[cn][ci]);
}
}
printf(" Numbers Inside Matrix> ");
for(ci = 0; ci<*colu; ci++)
{
for(cn=0; cn<10; cn++) printf(" %d", arp[cn][ci]);
}
}
void p3(void)
{ // >>>>main<<<<
int colu=0;
while(loop)
{
printf("\n Input The Quantity Of Columns To Use\n > ");
scanf("%d", &colu);
int arp[10][colu];
mat(&colu, arp);
felmnt(&colu, arp);
keepalive();
}
}
int main(void)
{
int ck = 0, ndx;
while(1)
{ // infinite loop
p3();
fflush(stdin);
printf("\nPause !!!");
getchar();
}
return 0;
}
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 4 years ago.
Improve this question
The program should have the following guidelines:
The numbers are read from the standard input.
The first number is the length of the sequence (n) followed by n numbers.(ie, if you put '54321' the length of the sequence is 5 numbers)
If n is 0 or negative, the program displays the message “Error_1” followed
by a new line on the standard input.
If the length is shorter than n, it displays “Error_2” followed by a new line and quits.
I'm finding point number 2 difficult
My code is:
#include <stdio.h>
int main() {
int i,j,k;
printf("Enter a Number:\n");
scanf("%d", &i);
if (i <= 0 ) {
printf("Error_1\n");
} else if(){
printF("Error_2\n")
}
}
#include <stdio.h>
int main() {
int i,j,k;
printf("Enter a Number:\n");
scanf("%d", &i);
if (i <= 0 ) {
printf("Error_1\n");
} else{
scanf("%d",&j);
k=0;
while(j>0)
{
k++;
j=j/10;
}
if(k<i)
printf("Error_2\n");
}
}
so what i did was, i found out the length of number entered and if the length does not match with provided length , it prints error2. i found out the length by continously dividing the number by 10 till it becomes 0.
Check this out. You can use log base 10 to compute the length of the sequence. Easier and clener.
To compile the code use -lm flag.See the following command:
gcc sampleFilename.c -lm
#include <stdio.h>
#include<math.h>
int main() {
int i,num, length;
printf("Enter a Number:\n");
scanf("%d", &i);
if (i <= 0 ) {
printf("Error_1\n");
}
else{
printf("Enter the number: ");
scanf("%d",&num);
length=(int) log10(num)+1;// compute the length of the number .. read about log10
if (length < i ) //length of the sequence is less than the number 'i'
printf("Error_2\n");
}
}
there is something you have to understand
your code:
"if (i <= 0 ) {
printf("Error_1\n");
} else if () {/* I'm talking about this*/
printF("Error_2\n")
}"
commend: /* its mean else if (its empty!!) you should write condition in the if, what you need to do its just use if and not else or else if
https://www.programiz.com/c-programming/c-if-else-statement*/
currect code below:
#include <stdio.h>
int main()
{
int num,cnt=0;
printf("Enter length: "\n);
scanf("%d", &num);
while(num > 10) //checking first number
{
cnt++;
num /= 10;
}
if(num <= 0)
{
printf("Error_01\n");
}
if(num == (count + 1)!)
{
printf("Error_02");
}
return 0;
}
Try this below code,
Hope this will work
#include <stdio.h>
int main()
{
int i,j,k;
printf("Enter any number: \n");
scanf("%d", &i);
k = 0;
if(i <= 0)
{
printf("Error_01\n");
}
printf("Enter value number: \n");
scanf("%d", &j);
while(j != 0) // check till first digit
{
k++;
j /= 10;
}
if(k != i)
{
printf("Error_02\n");
}
return 0;
}
by the way this was referred from
https://codeforwin.org/2016/10/c-program-to-count-number-of-digits-in-number.html
I have a program that uses a tail recursion function to add the value entered and all the previous values. For example if the user enters 3, the function calculates 3+2+1 and gets an answer of 6. However this only sometimes works.
Here is my code below:
int addNum(int n);
int main(int argc, char *argv[]) {
int num;
printf("Enter an integer greater than zero, (q to quit): \n");
while(scanf("%d", &num) > 0){
if(num < 0){
continue;
}
else if(num == 0){
printf("Answer = 0 \n");
}
else{
printf("Answer = %d \n", addNum(num));
}
printf("Enter a positive number: \n");
}
return 0;
}
int addNum(int n){
int answer;
if(n > 0){
answer = n += addNum(n - 1);
}
return answer;
}
The output I get from this code is very peculiar. For numbers 0-6 I get the right answers. Once I get to 7 and 8 both the answers are incorrect. I keep going and 9-12 the answers are correct again. Then 13 and 14 are incorrect. It keeps going back and fourth like this. I have absolutely no clue what is going on if someone could help. If it is something simple and wrong with my code please don't give me the answer but rather a hint at the problem.
I will post the output below so you can see what is going on. I decided to use pastebin for my output to save room. http://pastebin.com/DjJfxJAT
You don't initialize answer in addNum(), so whenever you call it with n == 0, undefined behavior occurs since you end up returning an uninitialized value. It's only by luck that you ever get the right answer.
I think this code itself explains things
int addNum(int n);
int answer=0;
int main(int argc, char *argv[]) {
int num;
printf("Enter an integer greater than zero, (q to quit): \n");
while(scanf("%d", &num) > 0){
if(num < 0){
continue;
}
else if(num == 0){
printf("Answer = 0 \n");
}
else{
answer=0;
printf("Answer = %d \n", addNum(num));
}
printf("Enter a positive number: \n");
}
return 0;
}
int addNum(int n){
if(n > 0){
answer = n += addNum(n - 1);
}
return answer;
}