1

Summary: I have 2 files cs330_Lab2.h and cs330_Lab2.c The cs330_Lab2.c contains my main while my cs330_Lab2.h contains my functions. My code works fine till I go to run printHELLO() function in cs330_Lab2.h. I think it is crashing potentially after I perform the scanf() function but I am not sure. As for logic, I am about 95% sure it is right as it is a function I translated from a java project.

This is my cs330_Lab2.h file:

#include <stdio.h>
#include <math.h>
#include <stdbool.h>



// this function takes in a int and finds the cube of every ud number leading up to it
// then it prints the string of that odd number
int cubeOfOdds(int n1){
    // declaring varables
    int i;
    int n = n1;
    for(i =1 ; i< n ; i++){
        if (i%2 != 0){
            int cubeI = i * i * i;
            printf("Cube of %d: " , i ,": ");
            printf("%d",cubeI);
            printf("\n");
        }
    }
    return 0;
}
bool isPrime(int n5) {

    bool isItPrime = 1;
    int i =2 ;
    for (i; i <= n5 / 2; ++i) {

        if (n5 % i == 0) {
            isItPrime = 0;

        }
    }

    return isItPrime;
}
bool isPowerOfTwo(int n)
{
    if (n == 0)
        return 0;
    while (n != 1) {
        if (n % 2 != 0)
            return 0;
        n = n / 2;
    }
    return 1;
}
int introToCS330 (int n2){
    int n = n2;
    if(n % 7 == 0){
        printf("UAB");
        printf("\n");
    } else if (n % 3 == 0){
        printf("CS");
        printf("\n");
    }else if (n2 % 3 == 0 && n2 % 7 == 0) {
        printf("UAB CS 330");
        printf("\n");
    } else if (isPrime(n2) && n2 != 3 && n2 != 7) {
        printf("Go Blazers");
        printf("\n");

    } else {
        int out = n2 * n2 * n2;
        printf("%d", out);
        printf("\n");
    }
    printf("\n");
    return 0;
}
int printHELLO (int n3){
    int i;
    for( i=0;i<=n3;i++)
    {
        if(isPowerOfTwo(i))
        {
            printf("HELLO");
        }else
        {
            printf("%d",i);
        }
    }



    return 0;
}
int paintGallons(float length, float width, float height){
    float sqft = (((length * height * 2)+(width * height * 2)+(length*width))/400);
    int neededGals = round(sqft);
    return neededGals;


}
 bool grader (float avg_exams, float avg_hw, int attendance)  {
    bool out = 0;
    if (attendance >= 20) {
        printf("FAIL");

        if (avg_exams > 70 && avg_hw > 70 && (avg_exams > 85 || avg_hw > 85) ){
            printf("PASS");
            out = 1;
        }
    }



    return out;
}

This is my cs330_Lab2.c File:

//
// Created by willb on 9/11/2022.
//
#include "cs330_Lab2.h"
int main(){


    printf("\n");
    printf("cubeOfOdds:");

    printf("Please enter a number:");
    int n1;
    scanf("%d",&n1);


    cubeOfOdds(n1);

    printf("\n");

    printf("introToJava:");
    printf("Please enter a positive integer: ");
    int n2;
    scanf("%d",&n2);
    introToCS330(n2);


    printf("printHELLO:");
    printf("Please enter a positive integer: ");
    int n3;
    scanf("%d",&n3);
    printHELLO(n3);


    int k = 10;
    int k1 = 12;
    int k2 = 8;
    int h =paintGallons(k, k1, k2);




    grader(72,88,22); // expected result PASS


    return 0;

}

This is the instructions for printHello: printHello (n) Write the function printHELLO that takes an integer n (n is equal or greater than 0) and prints a string with the integers 0 through n, inclusive, except that every power
of 2 is replaced by HELLO.

and the output, if int 3 is entered, should be: "0HELLOHELLO3" and the output, if int 7 is entered, should be: "0HELLOHELLO3HELLO567"

I am not asking you to solve it for me. I just need to know why it is crashing and how to fix it. I can go back and fix all logical errors once I get it to run.

  • 2
    Unrelated: split cs330_Lab2.h into two files (eg cs330_Lab2_tools.c and cs330_Lab2_tools.h). Keep **code** in the c file, keep **declarations** in the h file. Add a `#include "cs330_Lab2_tools.h"` to the c file. – pmg Sep 12 '22 at 10:51
  • I already have them separated into two files already one is called cs330_Lab2.h (this includes all the functions) the other is called cs330_Lab2.c ( this includes my main and #include "cs330_Lab2.h" ) – Will McCallum Sep 12 '22 at 10:58
  • Does this answer your question? [scanf() leaves the newline character in the buffer](https://stackoverflow.com/questions/5240789/scanf-leaves-the-newline-character-in-the-buffer) – Ken Y-N Sep 12 '22 at 10:59
  • are you saying that I should split up my "cs330_Lab2.h" file into two files called cs330_Lab2_tools.c and cs330_Lab2_tools.h? and if so do I put half of the functions in one and half of the functions in the other? – Will McCallum Sep 12 '22 at 11:00
  • 1
    Yes. Point is ... h files are not supposed to have code in them (**no** function or variable **definitions**, just function and variable **declarations**, and macros, ...) – pmg Sep 12 '22 at 11:01
  • 2
    I have problems finding the code which is relevant to the described exercise in all the shown code. Please provide a [mre]. Even some of the actually executed code contradicts the description. I am willing to help, but please make it easier. – Yunnosch Sep 12 '22 at 11:07
  • 1
    No, you should not put half of the functions in one file and the other half in the other ffie. You should put **declarations** in the .h file and **definitions** into the .c file. Headers are meant to provide same declarations to multiple files. If you add the function code, each file including the header will also get a copy of the functions or variables defined there. That should be avoided. – Gerhardh Sep 12 '22 at 11:32
  • You should run your program in a debugger. It should at the very least, show you where your program crashes (if it really crashes). Generally, what makes you think your program crashes? What output do you get, what input do you provide? You only show expected output in your question, not what you get. – Gerhardh Sep 12 '22 at 11:51
  • Does anyone have any good resources on getting user input in c while avoiding stack buffer overflow? – Will McCallum Sep 12 '22 at 11:56
  • 1
    You can make your prints include the newline: `printf("UAB"); printf("\n");` becomes `printf("UAB\n");`. You shouldn't add parameters to printf() that are unaccounted for: `printf("Cube of %d: " , i ,": ");`becomes `printf("Cube of %d: " , i);`. So in total `printf("Cube of %d: " , i ,": "); printf("%d",cubeI); printf("\n");` becomes `printf("Cube of %d: %d\n", i, cubeI);` – mmixLinus Sep 12 '22 at 11:59
  • 2
    Please [edit] and show an example of input and expected output. Format it properly. Also add the __verbatim__ output you get. – Jabberwocky Sep 12 '22 at 11:59
  • What value of `n3` do you get from the `scanf()`? – mmixLinus Sep 12 '22 at 12:02
  • I am trying to get n3 = int 3 from my scanf(). then I want to pass that n3 to printHELLO(n3) and get "0HELLOHELLO3" back. (**printHELLO() should return a variable type string**) – Will McCallum Sep 12 '22 at 12:08
  • I know my issue is related to stack buffering overflow. I am just new to C and cant figure out how to go about getting user input without running into this stack buffer overflow problom. – Will McCallum Sep 12 '22 at 12:10
  • @WillMcCallum please [edit] your question and put all relevant information into the questino and not in comments. Format it properly. – Jabberwocky Sep 12 '22 at 12:31

0 Answers0