my program does not continue compiling if I don't type any letter - cs50

I am solving the problem set1_ credit in cs50. the program woks fine, however after entering the credit card number, i need to enter any letter in the keyboard in order for the program to give me the answer.
here is my code
# include <cs50.h>
# include <stdio.h>
# include <math.h>
long credit(void);
int main(void)
{
long long num = credit();
long long num2 = num;
int c = log10(num), i, sum , sum1, sum2 = 0, digits[c], divid[c], remind[c], total[c], cards[c];
scanf("%d", &c);
for ( i = 0; i <= c ; i++)
{
digits[c-i] = num % 10;
num = num /10;
scanf("%d", &digits[c-i]);
}
if ( c % 2 != 0)
{
for ( i = 0 ; i <= c ; i++)
{
if ( i % 2 == 0 )
{
digits[i] = digits [i] * 2;
}
else
{
digits[i] = digits [i];
}
}
}
else
{
for ( i= 0 ; i <= c ; i++)
{
if ( i % 2 != 0)
{
digits [i] = digits[i] * 2;
}
else
{
digits[i] = digits[i];
}
}
}
for ( i = 0; i <= c; i++ )
{
remind[i] = digits[i] % 10;
}
//printf("\n");
for (i = 0; i <= c; i++)
{
divid[i] = digits[i] / 10;
}
for ( i = 0; i <= c; i++ )
{
total[i] = remind[i] + divid[i];
}
for ( i = 0; i <= c ; i++ )
{
sum = sum + total[i];
}
// recreate the card's number in a form of an array
for ( i = 0; i <= c ; i++)
{
cards[c-i] = num2 % 10;
num2 = num2 /10;
scanf("%d", &cards[c-i]);
}
// check the nature and the validity of a card
int cards1 = cards[1];
if (sum % 10 == 0 && cards[0] == 3)
{
if (c == 15 )
{
switch (cards [1])
{
case 4: printf("AMEX");
break;
case 7: printf("AMEX");
break;
}
}
//return 0;
printf("AMEX");
}
else if (cards[0] == 5 && sum % 10 == 0)
{
if (c == 16)
{
switch (cards [1])
{
case 1: printf("MASTERCARD");
break;
case 2: printf("MASTERCARD");
break;
case 3: printf("MASTERCARD");
break;
case 4: printf("MASTERCARD");
break;
case 5: printf("MASTERCARD");
break;
}
}
//return 0;
printf("MASTERCARD\n");
}
else if (cards[0] == 4 && sum % 10 == 0)
{
switch (c)
{
case 13: //printf("VISA\n");
break;
case 16: //printf("VISA\n");
break;
}
printf("VISA\n");
}
else
{
printf("INVALID\n");
}
printf("\n");
}
// get number of digits of an integer
long credit(void)
{
long long n;
do
{
n = get_long_long("Number: ");
}
while (log10(n) < 12 || log10(n) > 16);
return n;
}
I would be very grateful if anyone could help me solve this issue.
thanks in advance.

From man scanf:
The scanf() function reads input from the standard input stream stdin
The program is waiting for keyborad input (stdin) at one of the several scanf commands.
NB This question description is misleading because the program does compile, it (seemingly) stops running until keyboard input.

Related

Why is my integer variable being added to my other integer variable?

I am writing a program that counts the amount of letters and words in a string given by the user. For some reason, the number of words is being added to the number of letters. If there is 3 words in the sentence and 12 letters, then it says that there is 15 words. My code is below:
#include <cs50.h>
#include <string.h>
#include <stdio.h>
#include <ctype.h>
int storeLetters[] = {};
int storeWords[] = {};
// declare functions
int count_letters(string text);
int count_words(string text);
int main(void)
{
// ask user for text passage
string text = get_string("Text: ");
int numOfLetters = count_letters(text);
printf("%d",numOfLetters);
printf(" letters\n");
int numOfWords = count_words(text);
printf("%d",numOfWords);
printf(" words\n");
}
int count_letters(string text)
{
int amountOfLetters = 0;
for (int i = 0, n = strlen(text); i < n; i++)
{
if (isalpha(text[i]))
{
storeLetters[i] += 1;
amountOfLetters += storeLetters[i];
}
else
{
storeLetters[i] += 0;
amountOfLetters += storeLetters[i];
}
}
return amountOfLetters;
}
int count_words(string text)
{
int amountOfWords = 0;
for (int x = 0, n = strlen(text); x < n; x++)
{
if (text[x] == '?' || text[x] == '!' || text[x] == '.' || text[x] == ' ')
{
storeWords[x] += 1;
amountOfWords += storeWords[x];
}
else
{
storeWords[x] += 0;
amountOfWords += storeWords[x];
}
}
return amountOfWords;
}
storeLetters and storeWords are arrays, which you initialize to zero length. If you access storeLetters[0] (or any other index), you go past the end. It's not gonna work.
You don't need those variables at all. Just increment amountOfLetters directly.
int count_letters(string text)
{
int amountOfLetters = 0;
for (int i = 0, n = strlen(text); i < n; i++)
{
if (isalpha(text[i]))
{
amountOfLetters += 1;
}
}
return amountOfLetters;
}

(C++) How to round decimals like 1.05000?

I have a problem where in my algorhithm I should round the result down to 5 decimal places, with the zeros included even after the last number.
The only test-case not working in my algorhithm is:
Input:
milk 1
4
bread
meat
milk
aaaaa
Output:
1.05000 // and my output displays 1.5
For an example; my result of 1.05 should display as 1.05000 or 1.2 as 1.20000. Now, the rest of the algorhithm is working fine, so the only problem is the rounding part:
#include <iostream>
#include <string.h>
using namespace std;
int main()
{
char name[50];
cin >> name;
double price = 0;
cin >> price;
int N;
cin >> N;
char check_name[50];
double result = 0;
bool found = false;
double result_circle = 0;
int finally_found = 0
for (int k = 0; k < N; k++) {
cin >> check_name;
for (int i = 0; i < strlen(name); i++) {
if (name[i] == check_name[i]) {
found = true;
} else {
found = false;
break;
}
}
if (found) {
finally_found++;
break;
}
}
if (finally_found > 0) {
result = price + (price * 0.05);
} else {
result = price + (price * 0.18);
}
// here is where the problem starts
result_circle = result * 1000000; //temporarily expanding the number to the 6th decimal place
if ((int)result_circle % 10 > 4) { // checking if the 6th decimal place is bigger than 4
result_circle += 10; // increasing the 5th decimal place
}
result_circle = (int)result_circle / 10; // removing the 6th decimal place which we were checking
cout << (int)result_circle / 100000 << '.' << (int)result_circle % 100000; // here is the main problem, where 105000 % 100000 is seen as 5000 not 05000
return 0;
}
I assume the main problem here is that ‘105000 % 100000 = 5000’ because the 0 after the decimal point is unfortunately left out.
If anyone could display the simplest way to fix this problem it would be great.
#include <iomanip>
.
.
.
cout << fixed << setprecision(5) << result;
This is the code that worked for me, answered by _Bob.
The full code:
#include <iostream>
#include <string.h>
#include <iomanip>
using namespace std;
int main()
{
char name[50];
cin >> name;
double price = 0;
cin >> price;
int N;
cin >> N;
char check_name[50];
double result = 0;
bool found = false;
double result_circle = 0;
int finally_found = 0;
for (int k = 0; k < N; k++) {
cin >> check_name;
for (int i = 0; i < strlen(name); i++) {
if (name[i] == check_name[i]) {
found = true;
} else {
found = false;
break;
}
}
if (found) {
finally_found++;
}
}
if (finally_found > 0) {
result = price + (price * 0.05);
} else {
result = price + (price * 0.18);
}
cout << fixed << setprecision(5) << result;
return 0;
}

char in string is not in order wanted

I am a beginner and I need some help here. This program prints out the frequency of char in the string, e.g. if user enters zzaaa it prints out a3z2 and what I need to print is z2a3 since z is entered first before a. But I am having a hard time switching the order around. Thanks in advance!
int main
{
int ib, i=0, j=0, k=0;
int count[26] = {0};
char chh[3][10];
for (ib = 0; ib < 3; ib++) // get 3 input
gets(chh[ib]);
for (i = 0; i < 3; i++)
{
for (j = 0; j < 10; j++)
{
if (chh[i][j] >= 'a' && chh[i][j] <= 'z')
{
count[chh[i][j] - 'a']++;
}
}
for (k = 0; k < 26; k++)
{
if (count[k] != 0) // if array location is not equals to 0
printf("%c%d", k + 'a', count[k]);
}
memset(count, 0, sizeof(count)); //reset integer array
printf("\n");
}
It prints a before z because you arranged count from a to z by alphabetic priority not entering priority:
count[chh[i][j] - 'a']
if you want to print them by entering priority you should change it. there are several ways to do this. like this:
#include <stdio.h>
#include <string.h>
int main()
{
int ib, i=0, j=0,k=0, kk=0,c=0,found=0;
int count[26][2];
char chh[3][10];
for (ib = 0; ib < 3; ib++) // get 3 input
gets(chh[ib]);
printf("output is:\n");
for (i=0;i<26;i++)
{
count[i][0]=0;
count[i][1]=0;
}
for (i = 0; i < 3; i++)
{
for (j = 0; j < 10; j++)
{
if (chh[i][j] >= 'a' && chh[i][j] <= 'z')
{
found=0;
for (c=0;c<kk;c++)
if (count[c][0]==chh[i][j])
{
count[c][1]++;
found=1;
break;
}
if (!found)
{
count[c][0]=chh[i][j];
count[c][1]++;
kk++;
}
}
}
for (k = 0; k < 26; k++)
{
if (count[k][1] != 0) // if array location is not equals to 0
printf("%c%d", count[k][0], count[k][1]);
}
memset(count, 0, sizeof(count)); //reset integer array
printf("\n");
}
}

Merge multiple input strings to sort alphabetically

Help! Here I have a program, that sorts input strings alphabetically. The problem is that it sorts the entered strings seperately, but I need them merged and sorted together. What am i doing wrong?
int main (void)
{
char repeat;
do{
char string[128], string1[128], temp;
int n, i, j;
cout<<"\nEnter symbols: "<<endl;
gets(string);
n = strlen(string);
for (i=0; i<n-1; i++)
{
for (j=i+1; j<n; j++)
{
int s = tolower(string[i]) - tolower(string[j]);
if ( s == 0 )
{
s = string[i] - string[j];
}
if (s > 0)
{
temp = string[i];
string[i] = string[j];
string[j] = temp;
}
}
}
cout<<"\nSorted alphabetically: "<<endl;
printf("\n%s", string);
printf("\n");
cout<<"\nEnter more symbols: "<<endl;
gets(string1);
n = strlen(string1);
for (i=0; i<n-1; i++)
{
for (j=i+1; j<n; j++)
{
int s = tolower(string1[i]) - tolower(string1[j]);
if ( s == 0 )
{
s = string1[i] - string1[j];
}
if (s > 0)
{
temp = string1[i];
string1[i] = string1[j];
string1[j] = temp;
}
}
}
cout<<"\nSorted alphabetically: "<<endl;
the function below merges the strings, when they both have been already sorted
strcat(string, string1);
printf(\n%s", string);
printf("\n");
cout << "To repeat press j" << endl;
cin >> repeat;
}
while ( repeat == 'j' );
system("Pause");
return 0;
}
The easiest way is to merge the strings into a new one.
int main (void)
{
char repeat;
do{
char string[128], string1[128],string2[256], temp;
int n, i, j;
cout<<"\nEnter symbols: "<<endl;
gets(string);
cout<<"\nEnter more symbols: "<<endl;
gets(string1);
strcpy (string2,string);
strcat (string2,string1);
// rest of your code here. You only need to sort string2

C, convert hex number to decimal number without functions

i'm trying to convert hexadecimal number to decimal number. What i've come up so far is:
#include <unistd.h>
#include <stdio.h>
long convert(char *input, short int *status){
int length = 0;
while(input[length])
{
length++;
}
if(length = 0)
{
*status = 0;
return 0;
}
else
{
int index;
int converter;
int result = 0;
int lastNumber = length-1;
int currentNumber;
for(index = 0; index < length; index++){
if(index == 0)
{
converter = 1;
}
else if(index == 1)
{
converter = 16;
}
else{
converter *= 16;
}
if(input[lastNumber] < 45 || input[lastNumber] > 57)
{
*status = 0;
return 0;
}
else if(input[lastNumber] > 45 && input[lastNumber] < 48)
{
*status = 0;
return 0;
}
else{
if(input[lastNumber] == 45)
{
*status = -1;
return result *= -1;
}
currentNumber = input[lastNumber] - 48;
result += currentNumber * converter;
lastNumber--;
}
}
*status = -1;
return result;
}
}
int main(int argc, char **argv)
{
char *input=0;
short int status=0;
long rezult=0;
if(argc!=2)
{
status=0;
}
else
{
input=argv[1];
rezult=convert(input,&status);
}
printf("result: %ld\n", rezult);
printf("status: %d\n", status);
return 0;
}
Somehow i always get resoult 0. Ia am also not allowed to use any other outher functions (except printf). What could be wrong with my code above?
This:
if(dolzina = 0)
{
*status = 0;
return 0;
}
is not merely testing dolzina, it's first setting it to 0. This causes the else clause to run, but with dolzina equal to 0 which is not the expected outcome.
You should just use == to compare, of course.

Resources