C/C++ Thread

ScottALot

Active Member
Brand new to programming other than Visual BASIC and HTML... is there a list of programs that's like half-required to download to start programming in C/C++ [and JAVA]?
 

Cromewell

Administrator
Staff member
Brand new to programming other than Visual BASIC and HTML... is there a list of programs that's like half-required to download to start programming in C/C++ [and JAVA]?

An IDE helps but all you really need is a compiler, you can technically write the code in a plain text editor and use the command line to compile. For C/C++ on Windows DevC++ is ok, visual studio is as well. There may be other editors/compilers out there but those are the 2 I've used.

For Java a lot of people like Eclipse.
 

Troncoso

VIP Member
I have to say. Programming in C/C++ in linux has been a treat to me.especially since gedit has built in sytax highlighting and auto tabbing. And all you need to compile is gcc and g++ which I think are included. If now they are easy to install
 

Dystopia

Active Member
I'm guessing this is for simple interest (i.e. you have to pay off the interest accrued each year) instead of compound interest.

Check how you are handling the interest. Some of the calculations don't look right to me around your monthly payment comment around line 30.

Hmmm, I'll check it out. If found that when I try to call a function, it doesn't work, as in, the code within the function doesn't seem to be executed.
 

Cromewell

Administrator
Staff member
This is some sample output I get when I run your code:
What is the value of the loan? $100
For how long will you have the loan? 5
What percentage is your APY? 10
198.709
198.709
198.709
198.709
198.709
The function is doing something or it wouldn't retun any value.
 

Troncoso

VIP Member
Can someone maybe tell me why this wouldn't work right:

Code:
#include <stdio.h>
#include <math.h>

int main()
{
	printf("This program will accept a whole number and return it's factorial.\n Please enter a whole number: ");
	
	int number;
	scanf("%d", &number);
	
	while (number % 1 != 0)
	{
		printf("This value is not a whole number. please type a whole number: ");
		
		scanf("%d", &number);
	} 
	
	int total = number;
	int count;
	
	for (count = number - 1; count > 0; count--);
	{
		total = total * count;
	}
	
	printf("The factorial of %d is %d.\n", number, total);
	
	getchar();
	
	return 0;
}

When I run it, it just goes from the first printf statement to the last, as if it's not even doing the while loop or the for loop...No matter what I change I get the same outcome.

EDIT: Okay, I got it calculating the factorial, but the reason it seems to be skipping the while statement, is because when you type a decimal (ex. 5.2) it just truncates the .2 so it proves true. My problem is inputting a letter. When I input the letter 'd' and hit enter it says "The factorial of 6598644 is 0" I have no idea how it gets that number.
 
Last edited:

Cromewell

Administrator
Staff member
I think D is giving you weird values because you are then effectively using uninitialized memory.

For your loop, check the very start for your error, no complier will report it as a syntax error but it is.
 

Cromewell

Administrator
Staff member
You should really be validating the user input before trying to use it but it's never bad to initialize your variables.
 

Troncoso

VIP Member
Well now you've confused me. That's what I am trying to do. That's what the while statement is for, to validate the value. I'm trying to figure out why, no matter wat I type in, the while function accepts the value and doesn't catch my exception handler.
 

Cromewell

Administrator
Staff member
Oops didn't see it, instead of using number % 1 try using the actual typing function in ctype.h (cctype for c++ headers). You'll also have to clear the input stream of the invalid characters or it will keep trying to read them in.

edit: I hate input in C...I think the best way is to read the input as a string and then try to convert it to the type you need. I've always had trouble trying to clear out the unread characters.

edit2: something like this:
Code:
...
	int number;
    char input[250];
	
	fgets(input, sizeof input, stdin);
	
	while (sscanf(input,"%d",&number) != 1)
	{
		printf("This value is not a whole number. please type a whole number: ");
		
		scanf("%s", &input);
		sscanf(input, "%d", &number);
	} 
...
 
Last edited:

Troncoso

VIP Member
oh wow. Thank you. just a question about sscanf:

the first argument, that's the max size of the string right? why did you use input as the argument.
 

Cromewell

Administrator
Staff member
sscanf(const char * str, const char * format, ...)

The ... in the function call represents user managed parameters instead of compiler. This is how you can say sscanf(string, "%d %s %d", num1, str1, num2) or sscanf(string, "%d",num1) and have both run on the same code.

The first parameter to sscanf is the string you want to scan, the second parameter is what you are looking for and the n parameters after are the variables you want to store the results in.
 

Troncoso

VIP Member
Okay, I tried that and I get the same outcome:

Code:
troncoso@TroncosoUbuntu:~/Documents$ ./fact
This program will accept a whole number and return it's factorial.
Please enter a whole number: 5.2
5! = 120.


troncoso@TroncosoUbuntu:~/Documents$

I should be getting the error message for 5.2. When typing letters it works, but not decimals.
 

Cromewell

Administrator
Staff member
True enough, scanf (and any derivative) will try and return the requested type based on the pattern you are looking for. Since 5.2 does start with a valid number it returns that.

To prevent this you'd have to test the string you've read for a non-numeric character. The easiest way would be with a regular expression but C doesn't support it, you'd need a library.

I suppose you could read the value in as a float test for non-zero after the decimal. I'm sure there are other methods as well, I'm just drawing a blank on what they may be.
 
Last edited:

Dystopia

Active Member
This is some sample output I get when I run your code:

The function is doing something or it wouldn't retun any value.

That's what I got. I figured it out though, something was wrong with the way I wrote the code and the way I called the function/wrote the function. Either way, I figured it out.
 

Troncoso

VIP Member
Okay, I'm finding that I'll need to validate input for several upcoming projects, so I'm trying to write a function that will check that the number enter is neither a letter, letters, or numbers with decimal values. Heres what I got right now:

Code:
int intCheck(char value[])
{
    int length = strlen(value);
    int num, count;
    
    printf("%d", length);
    
    for (count = 0; count <= length; count++)
    {
        if (value[count] < 0 || value[count] > 9)
        { return 0; }
        else
        { continue; }
    return atoi(value);
    }

int main(int argc, char *argv[])
{
    int count;
    char num[256];
    int total = 1, hold = 0, add;
    printf("Enter a value: ");
    scanf("%c", num);
    
    while (intCheck(num) == 0)
    {
          printf("Invalid input. Please enter a whole number: ");
          scanf("%d", &num);
    }

1. Okay when I run this, I input an int, and it gives me the message "Invalid input. Please enter a whole number: " So I type in another int and it works like it is suppose to. Also, where I have it printing the size of the string, it always prints 3.

2. When I put in something that isn't an int (a decimal or letter) I get an infinite loop of "Invalid input. Please enter a whole number: "

Thanks again..
 

Cromewell

Administrator
Staff member
1. Other than using continue and multiple points for return (both you should try to avoid) the functions main problem lies in how you are checking the values. A char is treated as a number or a character depending on the context you are using it in. 'A' = 65, 'B' = 66, etc. When you compare the value to 0 through 9 you are actually looking for a series of nonprintable characters. 0-9 lie in the 48-57 range (ASCII values).

The continue also prevents your return atoi() call from ever happening.

2. Check your scanf calls, they are inconsistant :) I'd suggest using %s so that your strings get null terminated.
 

Troncoso

VIP Member
Thanks for the advice. I realized after I couldn't get it to work that it was a failing idea. So I scrapped it in favor of this:

Code:
int main()
{
	double number; 
	int count;
	unsigned long total; 
	char input[256];
	
	printf("This program will accept a whole number and return it's factorial.\nPlease enter a whole number: ");
	
	scanf("%lf",&number);
		
	while (number - (1 * trunc(number/1)) != 0)  
	{
		printf("This value is not a whole number. please type a whole number: ");
		
		scanf("%lf", &number);

I just used a double, checked it with the modulus formula and formatted the output at the end. Works perfectly, except I still can't figure out how to check for letters in the input.
 
Last edited:

Cromewell

Administrator
Staff member
You were on the rught track before, I have a working version of your function from last night. By going back to reading a number you are back to the trap that if characters end up in the input stream it gets stuck trying to read it.

Code:
int intCheck(char value[])
{
    int length = strlen(value);
    int count, num;
    
    //printf("length: %d, value: %s\n", length, value);
    
    for (count = 0; count < length; count++)
    {
        //printf("value[count]: %c, count: %d\n",value[count], count);
        if (value[count] < 48 || value[count] > 57)
        { return 0; }
    }

    num = atoi(value);
    
    return num;
}
 
Top