-
C++/C conversion to just C
Below is a program that generates random Addition for Subtraction problems depending on the user's choice. The user is prompted to input an answer and then it keeps your score. If you want to quit you just press zero. This is pretty much my first C program ever, I read the first eight chaps of my C/c++ book and along with supplementary online readings, made it. The thing is, it is suppose to be pure C! I was looking for advice on how to salvage this, please give me your wisdom :)
Code:
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <iomanip>
#include <string>
using namespace std;
int menu();
bool addition(int &reward); // function to return truth values
bool subtraction(int &reward);
int main()
{
bool Result, Finished = false;
int choice, reward, score = 0;
while(!Finished) // while finished is not true
{
cout << "\nscore:" << score; // print the score
{
choice = menu(); // calls for user input and enters it to switch
switch (choice){ // accesses the functions
case 0: Finished = true; break;
case 1: Result = addition(reward); break;
case 2: Result = subtraction(reward); break;
default : cout << "Error in input\n\n"; break;
}
if(Result == true){
score += reward; // score = score + reward
}
}
}
}
int menu() //main menu that gets called and allows for user input
{
int MenuChoice;
cout << "\n";
cout << "Enter 1 for addition\n";
cout << "Enter 2 for Subtraction\n";
cout << "Enter 0 to quit\n";
cout << "-----------------\n";
cout << "Choice: ";
cin >> MenuChoice; // user input
return MenuChoice; //return the choice to choice to select a case
}
bool addition(int &reward) //function that is called by the users selection
{
int answer;
int random1, random2;
reward = 1;
srand(time(NULL)); //generates fresh numbers each time.
random1 = 1+(rand()%99); // from 1-99
random2 = 1+(rand()%99); // from 1-99
cout << "\n" << random1 << " + " << random2 << " = "; //code to format the random math problem
cin >> answer; // user input of answer
{
if (answer == (random1+random2)){
cout << "Congratulations\n\n";
return true;// tells result whether to add a pt or not
} //if user is correct than the score is incremented
else{
cout << "Incorrect\n\n";} //if not than the score is not incremented
return false;
}
}
bool subtraction(int &reward) // follows same model as previous function, but instead subtraction.
{
int answer;
int random1, random2;
reward = 1;
srand(time(NULL));
random1 = 1+(rand()%99); // from 1-99
random2 = 1+(rand()%99); // from 1-99
cout << "\n" << random1 << " - " << random2 << " = ";
cin >> answer;
if (answer == (random1-random2)){
cout << "Congratulations\n\n";
return true; // tells result whether to add a pt or not
}
else{
cout << "Incorrect\n\n";}
return false;
}
-
Re: C++/C conversion to just C
For starters, indent your code properly. Next, it seems that the main C++ thing that you are using here is C++ I/O streams, so you just need to change them to use C I/O, e.g.,
Code:
cout << "\n" << random1 << " - " << random2 << " = ";
would become:
Code:
printf("\n%d - %d = ", random1, random2);
Of course, you should then remove the inclusions of <iostream> and <iomanip>. It looks like you are not using std::string, so you can just remove the inclusion for <string>.
-
Re: C++/C conversion to just C
If it were me, I'd compile it as a C program and see what the compiler complained about, then make appropriate changes.
-
Re: C++/C conversion to just C
any ideas on how to replace the bool with a C equivalent?
-
Re: C++/C conversion to just C
The normal C-way is just an int. A non-zero value is true and a zero value is false.
-
Re: C++/C conversion to just C
Okay so I swapped out the bools and couts and cin with ints, printfs and scanf. It wont compile saying that their is something wrong with the &reward int the functions
Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int menu();
int addition(int &reward); // function to return truth values
int subtraction(int &reward);
int main()
{
int Result, Finished = 0;
int choice, reward, score = 0;
while(Finished != 1); // while finished is not true
{
printf("\nscore: %d", score); // print the score
{
choice = menu(); // calls for user input and enters it to switch
switch (choice){ // accesses the functions
case 0: Finished = 1; break;
case 1: Result = addition(reward); break;
case 2: Result = subtraction(reward); break;
default : printf("Error in input\n\n"); break;
}
if(Result == 1){
score += reward; // score = score + reward
}
}
}
}
int menu() //main menu that gets called and allows fr user input
{
int MenuChoice;
printf("\n");
printf("Enter 1 for addition\n");
printf("Enter 2 for Subtraction\n");
printf("Enter 0 to quit\n");
printf("-----------------\n");
printf("Choice: ");
scanf("%d", &MenuChoice); // user input
return MenuChoice; //return the choice to choice to select a case
}
int addition(int &reward) //function that is called by the users selection
{
int answer;
int random1, random2;
reward = 1;
srand(time(NULL)); //generates fresh numbers each time.
random1 = 1+(rand()%99); // from 1-99
random2 = 1+(rand()%99); // from 1-99
printf("\n%d - %d = ", random1, random2); //code to format the random math problem
scanf("%d", &answer); // user input of answer
{
if (answer == (random1+random2)){
printf("Congratulations\n\n");
return 1;// tells result whether to add a pt or not
} //if user is correct than the score is incremented
else{
printf("Incorrect\n\n");} //if not than the score is not incremented
return 0;
}
}
int subtraction(int &reward) // follows same model as previous function, but instead subtraction.
{
int answer;
int random1, random2;
reward = 1;
srand(time(NULL));
random1 = 1+(rand()%99); // from 1-99
random2 = 1+(rand()%99); // from 1-99
printf("\n%d - %d = ", random1, random2);
scanf("%d", &answer);
if (answer == (random1-random2)){
printf("Congratulations\n\n");
return 1; // tells result whether to add a pt or not
}
else{
printf("Incorrect\n\n");}
return 0;
}
-
Re: C++/C conversion to just C
C doesn't support C++ references so they have to be replaced with pointers instead.
-
Re: C++/C conversion to just C
Quote:
Originally Posted by
chucker
Below is a program that generates random Addition for Subtraction problems depending on the user's choice. The user is prompted to input an answer and then it keeps your score. If you want to quit you just press zero. This is pretty much my first C program ever, I read the first eight chaps of my C/c++ book
What book teaches C/C++?
C and C++ are two separate languages -- either the book teaches C++, or the book teaches C. I know of no book that teaches both languages, and it would be foolhardy (IMO) to have such a book due to the differences in the languages, what is considered legal/illegal syntax, programming paradigms, etc. Such a book, if it exists, would only confuse the programmer trying to learn one language over the other.
Second, since the assignment sounds straightforward, pretend your C++ program doesn't exist. Write it from scratch using 'C'.
Regards,
Paul McKenzie
-
Re: C++/C conversion to just C
Quote:
Originally Posted by
chucker
any ideas on how to replace the bool with a C equivalent?
Quote:
Originally Posted by
S_M_A
The normal C-way is just an int. A non-zero value is true and a zero value is false.
C99 introduced 'bool' as a type, along with 'true' and 'false', of course. Although they are just macros under the hood, using them makes the program semantics clearer.
Just make sure to #include <stdbool.h> to use them.
-
Re: C++/C conversion to just C
There has been a long debate about the macro version of bool in C being good or not and I'm in the not good club.
-
Re: C++/C conversion to just C
Quote:
Originally Posted by
Paul McKenzie
What book teaches C/C++?
C and C++ are two separate languages -- either the book teaches C++, or the book teaches C. I know of no book that teaches both languages, and it would be foolhardy (IMO) to have such a book due to the differences in the languages, what is considered legal/illegal syntax, programming paradigms, etc. Such a book, if it exists, would only confuse the programmer trying to learn one language over the other.
Book is C how to program
it does C then C++ later.
-
Re: C++/C conversion to just C
Quote:
Originally Posted by
chucker
Book is C how to program
it does C then C++ later.
That is no different than a book that teaches both FORTRAN and Pascal, assuming that FORTRAN is a prerequisite to learning Pascal. It makes no sense IMO.
You're either learning C or learning C++. You're either thinking in 'C' or thinking in C++.
See this link about learning 'C' as a prerequisite to learning C++ (which I would think this book is attempting to do):
http://www.parashift.com/c++-faq-lit...n-c-first.html
So again, pretend your program you've written in C++ does not exist at all, and you're a brand new programmer to the 'C' world.
Regards,
Paul McKenzie
-
Re: C++/C conversion to just C
Quote:
Originally Posted by
S_M_A
C doesn't support C++ references so they have to be replaced with pointers instead.
Any advice on how to implement the pointers as a replacement? I know the basics of them, but not quite how to apply them to my existing program. I had an attempt below to use pointers but failed hard.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int menu();
void addition(int *reward); // function to return truth values
void subtraction(int *reward);
int main()
{
int Result, Finished = 0;
int choice, score = 0;
int rewards = 0;
while(Finished != 1); // while finished is not true
{
printf("\nscore: %d", score); // print the score
{
choice = menu(); // calls for user input and enters it to switch
switch (choice){ // accesses the functions
case 0: Finished = 1; break;
case 1: addition(&rewards); break;
case 2: subtraction(&rewards); break;
default : printf("Error in input\n\n"); break;
}
if(rewards == 1){
score += rewards; // score = score + reward
}
}
}
}
int menu() //main menu that gets called and allows fr user input
{
int MenuChoice;
printf("\n");
printf("Enter 1 for addition\n");
printf("Enter 2 for Subtraction\n");
printf("Enter 0 to quit\n");
printf("-----------------\n");
printf("Choice: ");
scanf("%d", &MenuChoice); // user input
return MenuChoice; //return the choice to choice to select a case
}
void addition(int *reward) //function that is called by the users selection
{
int answer;
int random1, random2;
srand(time(NULL)); //generates fresh numbers each time.
random1 = 1+(rand()%99); // from 1-99
random2 = 1+(rand()%99); // from 1-99
printf("\n%d - %d = ", random1, random2); //code to format the random math problem
scanf("%d", &answer); // user input of answer
{
if (answer == (random1+random2)){
printf("Congratulations\n\n");
*reward++;// tells result whether to add a pt or not
} //if user is correct than the score is incremented
else{
printf("Incorrect\n\n");} //if not than the score is not incremented
}
}
void subtraction(int *reward) // follows same model as previous function, but instead subtraction.
{
int answer;
int random1, random2;
srand(time(NULL));
random1 = 1+(rand()%99); // from 1-99
random2 = 1+(rand()%99); // from 1-99
printf("\n%d - %d = ", random1, random2);
scanf("%d", &answer);
if (answer == (random1-random2)){
printf("Congratulations\n\n");
*reward++;// tells result whether to add a pt or not
}
else{
printf("Incorrect\n\n");}
}
-
Re: C++/C conversion to just C
Quote:
Originally Posted by
chucker
Any advice on how to implement the pointers as a replacement? I know the basics of them, but not quite how to apply them to my existing program
Well, if you know the basics, then that would include knowing how to pass pointers to functions.
Your book doesn't discuss passing pointers to functions? As a matter of fact, look at your current code:
Code:
scanf("%d", &MenuChoice); // user input
What do you think is happening there in red?
Regards,
Paul McKenzie
-
Re: C++/C conversion to just C
okay i decided to take a different approach with globals instead of pointers. It compiles, but the screen is black. Any input in this case?
Code:
int menu();
int addition(int reward); // function to return truth values
int subtraction(int reward);
int main()
{
int Result, Finished = 0;
int choice, score = 0;
int reward = 0;
while(Finished != 1); // while finished is not true
{
printf("\nscore: %d", score); // print the score
{
choice = menu(); // calls for user input and enters it to switch
switch (choice){ // accesses the functions
case 0: Finished = 1; break;
case 1: Result = addition(reward); break;
case 2: Result = subtraction(reward); break;
default : printf("Error in input\n\n"); break;
}
if(Result == 1){
score += reward; // score = score + reward
}
}
}
}
int menu() //main menu that gets called and allows fr user input
{
int MenuChoice;
printf("\n");
printf("Enter 1 for addition\n");
printf("Enter 2 for Subtraction\n");
printf("Enter 0 to quit\n");
printf("-----------------\n");
printf("Choice: ");
scanf("%d", &MenuChoice); // user input
return MenuChoice; //return the choice to choice to select a case
}
int addition(int reward) //function that is called by the users selection
{
int answer;
int random1, random2;
srand(time(NULL)); //generates fresh numbers each time.
random1 = 1+(rand()%99); // from 1-99
random2 = 1+(rand()%99); // from 1-99
printf("\n%d - %d = ", random1, random2); //code to format the random math problem
scanf("%d", &answer); // user input of answer
{
if (answer == (random1+random2)){
printf("Congratulations\n\n");
return reward = 1;// tellsesult whether to add a pt or not
} //if user is correct than the score is incremented
else{
printf("Incorrect\n\n");} //if not than the score is not incremented
return 0;
}
}
int subtraction(int reward) // follows same model as previous function, but instead subtraction.
{
int answer;
int random1, random2;
srand(time(NULL));
random1 = 1+(rand()%99); // from 1-99
random2 = 1+(rand()%99); // from 1-99
printf("\n%d - %d = ", random1, random2);
scanf("%d", &answer);
if (answer == (random1-random2)){
printf("Congratulations\n\n");
return reward = 1;// tells result whether to add a pt or not
}
else{
printf("Incorrect\n\n");}
return 0;
}
-
Re: C++/C conversion to just C
Quote:
Originally Posted by
chucker
okay i decided to take a different approach with globals instead of pointers.
Why?
The way you learn something is to write a small program instead of trying to shoehorn "learning" code into a larger, more complex program.
Code:
#include <stdio.h>
void ptrTest(int *p)
{
*p = 20;
}
int main()
{
int x = 10;
printf( "The value of x = %d\n", x );
ptrTest( &x );
printf( "The value of x = %d\n", x );
}
OK, so what don't you understand about the above program? Do you see that the value of x changes to 20?
That's why I pointed you to your own code with scanf() as a usage of pointers. Look at the call to ptrTest -- does it look familiar?
Also, global variables should be avoided.
Quote:
It compiles, but the screen is black. Any input in this case?
Please format your code a little better than you have now.
Code:
int menu();
int addition(int reward); // function to return truth values
int subtraction(int reward);
int main()
{
int Result, Finished = 0;
int choice, score = 0;
int reward = 0;
while(Finished != 1); // while finished is not true
{
printf("\nscore: %d", score); // print the score
{
choice = menu(); // calls for user input and enters it to switch
switch (choice){ // accesses the functions
case 0: Finished = 1; break;
case 1: Result = addition(reward); break;
case 2: Result = subtraction(reward); break;
default : printf("Error in input\n\n"); break;
}
if(Result == 1){
score += reward; // score = score + reward
}
}
}
}
int menu() //main menu that gets called and allows fr user input
{
int MenuChoice;
printf("\n");
printf("Enter 1 for addition\n");
printf("Enter 2 for Subtraction\n");
printf("Enter 0 to quit\n");
printf("-----------------\n");
printf("Choice: ");
scanf("%d", &MenuChoice); // user input
return MenuChoice; //return the choice to choice to select a case
}
int addition(int reward) //function that is called by the users selection
{
int answer;
int random1, random2;
srand(time(NULL)); //generates fresh numbers each time.
random1 = 1+(rand()%99); // from 1-99
random2 = 1+(rand()%99); // from 1-99
printf("\n%d - %d = ", random1, random2); //code to format the random math problem
scanf("%d", &answer); // user input of answer
{
if (answer == (random1+random2)){
printf("Congratulations\n\n");
return reward = 1;// tellsesult whether to add a pt or not
} //if user is correct than the score is incremented
else{
printf("Incorrect\n\n");} //if not than the score is not incremented
return 0;
}
}
int subtraction(int reward) // follows same model as previous function, but instead subtraction.
{
int answer;
int random1, random2;
srand(time(NULL));
random1 = 1+(rand()%99); // from 1-99
random2 = 1+(rand()%99); // from 1-99
printf("\n%d - %d = ", random1, random2);
scanf("%d", &answer);
if (answer == (random1-random2)){
printf("Congratulations\n\n");
return reward = 1;// tells result whether to add a pt or not
}
else{
printf("Incorrect\n\n");
}
return 0;
}
Also, are you using a debugger to debug your code?
Regards,
Paul McKenzie
-
Re: C++/C conversion to just C
Quote:
Originally Posted by
chucker
It compiles,
Now we get into the danger of using C without properly learning it, and the reason why I stated to learn C from scratch, as if you didn't know C++. Yes, C++ borrows most basic syntax from 'C', but this is where you get into trouble, since C++ doesn't have the issues I'll explain below:
Code:
printf("\nscore: %d", score); // print the score
Where do you #include <stdio.h>? You didn't include it. So what happens when you compile the code using 'C'? The compile is successful, but you wind up potentially shooting yourself in the foot.
The 'C' compiler assumes the function returns an int, and that the parameters you're providing are correct. But this is the danger -- if you call functions that do not return an int, or you call functions where the parameters are actually different types and/or number of parameters, then your code can exhibit undefined behaviour.
For example, this code compiles, but what will it do?
Code:
int main()
{
int x;
x = sqrt( "abc123", 343, 53243 );
}
This 'C' code compiles also . The problem is that in reality, the sqrt() function returns a double, and takes a single double parameter. So what happens when you run this program? Who knows? Maybe your computer blows up. The difference in C++ is that there is no way the above code could have compiled, but C says "OK, I guess you know what you're doing".
The way you avoid this is to introduce the prototype to sqrt() and the easiest way to do that is to #include <math.h>
Code:
#include <math.h>
int main()
{
int x;
x = sqrt( "abc123", 343, 53243 );
}
Now the code will not compile successfully in 'C', since the prototype for sqrt() is in math.h, and the 'C' compiler can clearly see you're calling the sqrt() function incorrectly.
So by not #including all the proper headers and/or prototyping your functions, your program can easily become ill-formed. That's why I've always emphasized to posters posting 'C' code that they must post all header files that are included -- no shortcuts, no snippets, etc.
Regards,
Paul McKenzie
-
Re: C++/C conversion to just C
That was a copy and paste fault, stdio.h was there. thanks for the help so far, i am learning things. I went back to pointers and thought i applied them more correctly, but the screen is still just blank when i run it. I use codeblocks, so yes about the debugger.
Also thanks for the organization, will model the rest of my programs from that.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int menu();
void addition(int *Preward); // function to return truth values
void subtraction(int *Preward);
int main()
{
int Finished = 0;
int choice, score = 0;
int reward = 0;
while(Finished != 1); // while finished is not true
{
printf("\nscore: %d", score); // print the score
{
choice = menu(); // calls for user input and enters it to switch
switch (choice){ // accesses the functions
case 0: Finished = 1; break;
case 1: addition(&reward); break;
case 2: subtraction(&reward); break;
default : printf("Error in input\n\n"); break;
}
if(reward == 1){
score += reward; // score = score + reward
}
}
}
}
int menu() //main menu that gets called and allows fr user input
{
int MenuChoice;
printf("\n");
printf("Enter 1 for addition\n");
printf("Enter 2 for Subtraction\n");
printf("Enter 0 to quit\n");
printf("-----------------\n");
printf("Choice: ");
scanf("%d", &MenuChoice); // user input
return MenuChoice; //return the choice to choice to select a case
}
void addition(int *Preward) //function that is called by the users selection
{
int answer;
int random1, random2;
srand(time(NULL)); //generates fresh numbers each time.
random1 = 1+(rand()%99); // from 1-99
random2 = 1+(rand()%99); // from 1-99
printf("\n%d - %d = ", random1, random2); //code to format the random math problem
scanf("%d", &answer); // user input of answer
{
if (answer == (random1+random2)){
printf("Congratulations\n\n");
*Preward = 1;// tellsesult whether to add a pt or not
} //if user is correct than the score is incremented
else{
printf("Incorrect\n\n");} //if not than the score is not incremented
}
}
void subtraction(int *Preward) // follows same model as previous function, but instead subtraction.
{
int answer;
int random1, random2;
srand(time(NULL));
random1 = 1+(rand()%99); // from 1-99
random2 = 1+(rand()%99); // from 1-99
printf("\n%d - %d = ", random1, random2);
scanf("%d", &answer);
if (answer == (random1-random2)){
printf("Congratulations\n\n");
*Preward = 1;// tells result whether to add a pt or not
}
else{
printf("Incorrect\n\n");
}
}
-
Re: C++/C conversion to just C
Quote:
Originally Posted by
chucker
That was a copy and paste fault, stdio.h was there.
That's why it's important to make sure everything in your post is included, and especially the case if it's a 'C' program.
Quote:
the screen is still just blank when i run it. I use codeblocks, so yes about the debugger.
Single-step through your program using the debugger and see where it goes wrong.
Regards,
Paul McKenzie
-
Re: C++/C conversion to just C
oh shoot i always thought debugger just referred to compile errors.
I am trying to use it. I clicked debug and my program turned on immediatly blank. How do i get the option to step through it? its greyed out on the debugger bar.
-
Re: C++/C conversion to just C
I dont see a reason why the first printf statement cannot be displayed. Finished is clearly not equal to 1 yet so "score: 0" should be able to display at least. Its compiling with zero errors i mean cmon.
-
Re: C++/C conversion to just C
You need to set a break point. From there, you can step through your code.
-
Re: C++/C conversion to just C
I put a break point at the end of my main function. Nothing happens when I click debug except a black execution window. It doesnt give me the option to step thru still, its greyed out
-
Re: C++/C conversion to just C
Quote:
Originally Posted by
chucker
I put a break point at the end of my main function. Nothing happens when I click debug except a black execution window. It doesnt give me the option to step thru still, its greyed out
Set a break point at the beginning of main() (the first executable line), not at the end.
What is probably happening is that your program is running all the way until the end, which is where you set the breakpoint.
Quote:
Its compiling with zero errors i mean cmon.
Compiling with no errors has nothing to do with how your program will run.
Compiling with no errors means that your program is syntactically correct -- it doesn't mean your program is logically correct.
Regards,
Paul McKenzie
-
Re: C++/C conversion to just C
set break point right after main. absolutely nothing happens, nothing updates on the watches. Zip.
-
Re: C++/C conversion to just C
Quote:
Originally Posted by
chucker
set break point right after main. absolutely nothing happens, nothing updates on the watches. Zip.
Did you build your program using the correct debugging options? The debugger that is being used is gdb, and if you don't build your program with debug information, the debugger isn't going to work.
Regards,
Paul McKenzie
-
Re: C++/C conversion to just C
If you set a break point at the beginning and the program doesn't pause at that break point, then you have done something wrong. You should just be able to press F8 in Code::Blocks to run your program in debug mode. If you don't have that option, you need to create a project and add your file to that project.
-
Re: C++/C conversion to just C
its on gdb, its apart of a C project file, no matter where I put break points it wont give me any debug info in watches.
-
Re: C++/C conversion to just C
Did you set any variables to watch?
-
Re: C++/C conversion to just C
at the beginning of main when i declare all of those ints like "int choice" I right clicked them and selected find implementation/declaration and it says choice is not found, along with all the others.
Code:
/*---------------------------------------------------------------
* Programmer: Charles Cunningham
* Date: 2/5/2012
* Name: Hw#1
* Description: This program generates addition or subtraction problems
based on the users choice. Then the user must guess the correct answer. If correct the user gets a point added to his score
---------------------------------------------------------------*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
int menu();
void addition(int *Preward); // function to return truth values
void subtraction(int *Preward);
int main()
{
int Finished = 0;
int choice, score = 0;
int reward = 0;
while(Finished != 1); // while finished is not true
{
printf("\nscore: %d", score); // print the score
{
choice = menu(); // calls for user input and enters it to switch
switch (choice){ // accesses the functions
case 0: Finished = 1; break;
case 1: addition(&reward); break;
case 2: subtraction(&reward); break;
default : printf("Error in input\n\n"); break;
}
if(reward == 1){
score += reward; // score = score + reward
}
}
}
return 0;
}
int menu() //main menu that gets called and allows fr user input
{
int MenuChoice;
printf("\n");
printf("Enter 1 for addition\n");
printf("Enter 2 for Subtraction\n");
printf("Enter 0 to quit\n");
printf("-----------------\n");
printf("Choice: ");
scanf("%d", &MenuChoice); // user input
return MenuChoice; //return the choice to choice to select a case
}
void addition(int *Preward) //function that is called by the users selection
{
int answer;
int random1, random2;
srand(time(NULL)); //generates fresh numbers each time.
random1 = 1+(rand()%99); // from 1-99
random2 = 1+(rand()%99); // from 1-99
printf("\n%d - %d = ", random1, random2); //code to format the random math problem
scanf("%d", &answer); // user input of answer
{
if (answer == (random1+random2)){
printf("Congratulations\n\n");
*Preward = 1;// tellsesult whether to add a pt or not
} //if user is correct than the score is incremented
else{
printf("Incorrect\n\n");} //if not than the score is not incremented
}
}
void subtraction(int *Preward) // follows same model as previous function, but instead subtraction.
{
int answer;
int random1, random2;
srand(time(NULL));
random1 = 1+(rand()%99); // from 1-99
random2 = 1+(rand()%99); // from 1-99
printf("\n%d - %d = ", random1, random2);
scanf("%d", &answer);
if (answer == (random1-random2)){
printf("Congratulations\n\n");
*Preward = 1;// tells result whether to add a pt or not
}
else{
printf("Incorrect\n\n");
}
}
-
Re: C++/C conversion to just C
Set a break point at
and step from there. The problem should show up after a couple of steps. Also...your formatting for the switch statement...seems rather odd to me. I would do something like:
Code:
switch (choice)
{
// accesses the functions
case 0:
Finished = 1;
break;
case 1:
addition(&reward);
break;
case 2:
subtraction(&reward);
break;
default :
printf("Error in input\n\n");
break;
}
Sure, it is more lines of code, but imagine each of the cases requiring 10 instructions. I think the way I have it above would be more clear. But, that is just a personal preference.
-
Re: C++/C conversion to just C
But in my watches all it says is
Function arguements
Finished = no symbol "finished" in current context
choice = no symbol.... etc.
what is that telling me?? i declared them they are symbolizing something, so it should work right?
-
Re: C++/C conversion to just C
At this point, I'd review how you created your "project": Is the file you are editing actually part of the project? Are you sure you are editing the correct "main.c"?
It sounds like your program is compiling, but not the file you are writing. that, or not running the proper objects.
When these errors come up, and especially on small scale projects, I recommend you use "rebuild everything", just to make sure.
-
Re: C++/C conversion to just C
Consider the following:
Code:
int main()
{
int foo;
bar();
}
void bar()
{
// I have no knowledge of "foo"
}
This is what the message is getting at. Are you able to step through the code at all? I was able to step through the code, fix the error I found with the debugger, and run your code.
One thing you can try is this:
File > New > Project > Console applicate > Run through the wizard, name your project. Then, you will have new project. Just copy and paste your code into the new main.c file of that project. Build it, and try debugging that. Like monarch dodra suggested, your project may be set up incorrectly.
-
Re: C++/C conversion to just C
ok the problem was just at the while loop. had to get rid of ; and made it == instead of =. fml hahahaha.
well thank you everyone for coming on this journey with me. Conversion to C using pointers success!
On a final note does anyone know how to make the case selection letter selection like A) B) C) instead of 1 2 3 ?
-
Re: C++/C conversion to just C
Hello..good day..can you help me,about this? c++ to c...
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <iomanip>
using namespace std;
void EmployeeInfo ();
void input();
void printstars ();
void paycompute();
void date_coverage();
//global variables
string E_code,E_name,E_record,E_level,d_coverage;;
double perday,T_Hours,T_Mins;
int main(){
do{
system("cls");
//Accept input from user
cout<<"Enter Employee ID: ";
getline(cin,E_code);
// opens file for input
ifstream inFile("employee.txt",ios::in);
// if input file can not be open enter the fail state
if(!inFile) {
cout << "Can not open input file" << endl;
cout << "The program will terminate" << endl;
system("pause");
exit(0);
}
//search for employee code
do{
getline(inFile,E_record);
int location = E_record.find(E_code);
if(location>0)
{
E_code = E_record.substr(location,8);
E_level = E_record.substr(E_record.length()-1,1);
E_name = E_record.substr(0,location-1);
break;
}
}while(!inFile.eof());
if (E_level =="1") perday = 380;
if (E_level =="2") perday = 450;
if (E_level =="3") perday = 550;
//Display Records
if (E_name==""){
cout<<"Record not Found!\n\n\n";
system("pause");
exit(0);
}
else {
EmployeeInfo ();
input();
date_coverage();
EmployeeInfo ();
paycompute();
cout<<"\n\n";
system("Pause");
}
}while(true);
}//END
//MY USER-DEFINED FUNCTIONS
void EmployeeInfo (){
system("cls");
printstars ();
cout<<"\n\nEmployee Name :\t"<<E_name;
cout<<"\nEmployee Code :\t"<<E_code;
cout<<"\nEmployee Level:\t"<<"Level "<<E_level;
cout<<"\nEmployee Rate :\t"<<"Php"<<perday<<".00/day";
printstars ();
}
void input ()
{
// declare variables --------
int hrs_in[5],hrs_out[5], min_in[5],min_out[5],x,TH_inout[5];
char colon;
string days []={"Monday","Tuesday","Wednesday","Thursday","Friday"};
// accept input -------
cout<<"\n\nEnter the time using the following format(hour:minutes)\n";
for(x=0;x<5;x++){
cout<<"\n\nEnter Log-in for "<<days[x]<<" : ";
cin>>hrs_in[x]>>colon>>min_in[x];
cout<<"Enter Log-out for "<<days[x]<<" : ";
cin>>hrs_out[x]>>colon>>min_out[x];
}
//write data to text files ------------
ofstream out("dtr.txt",ios::out);
out<<E_code;
for(x=0;x<5;x++){
out<<","<<hrs_in[x]<<":"<<min_in[x]<<","<<hrs_out[x]<<":"<<min_out[x];
} // out<<"\n\nTotal Working Hours: "<<T_Hours<<" Hrs";
cout<<"Files successfully written to dtr.txt files!";
cout<<"\n\n";
system("pause");
//compute number of Total Hours and Minutes --------
for(x=0;x<5;x++){
if(hrs_in[x]<8)
{hrs_in[x]=8;
min_in[x]=00;
}
if(hrs_in[x]>=12 && hrs_in[x]<13)
{
hrs_in[x]=13;
min_in[x]=00;
}
if(hrs_in[x]>=12 && hrs_in[x]<=13)
{TH_inout[x] = (hrs_out[x]-hrs_in[x]);}
else
{TH_inout[x] = (hrs_out[x]-hrs_in[x])-1;}
T_Hours =T_Hours+TH_inout[x];
}
}
void printstars (){
cout<<"\n=================================================";
}
void date_coverage(){
system("cls");
printstars();
cout<<"\nEnter Payroll Date of Coverage: ";
getline(cin,d_coverage);
getline(cin,d_coverage);
}
void paycompute(){
cout<<"\n\nDate of Coverage\t: "<<d_coverage;
cout<<"\nTotal Work Hours\t: "<<T_Hours<<".00 Hrs";
cout<<"\nWeekly Regular Salary\t: "<<"Php"<<((T_Hours/8)*perday)<<".00";
cout<<"\n";
printstars ();
cout<<"\n\n";
}
thanks
-
Re: C++/C conversion to just C
Please format your code using [coe] tags as what you have posted is almost unreadable. use Go Advanced, select code then click '#'.
Help you about what? You haven't asked a question! If you want to convert this c++ code to c (why??) then as a starter for 10 you need to replace all references to the c++ classes (cin, cout, ifstream etc) with appropriate code using just the c library functions (printf, fread etc).
-
Re: C++/C conversion to just C
Quote:
Originally Posted by
Charmmanuel
Hello..good day..can you help me,about this? c++ to c...
Did you read the whole thread? :confused:
There is a lot of opinions what and how to achieve it. What is the problem?
-
Re: C++/C conversion to just C
hello.. Im still a student... I need help on that code..to make it c function not c++ ..
-
Re: C++/C conversion to just C
Quote:
Originally Posted by
Charmmanuel
hello.. Im still a student... I need help on that code..to make it c function not c++ ..
I am confused...
What happens? "still a student" cannot read? "still a student" has no idea how tocompile C file? :confused:
-
Re: C++/C conversion to just C
I just need someone who could help me about converting this C++ TO C FUNCTION.. this codes:
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <iomanip>
using namespace std;
void EmployeeInfo ();
void input();
void printstars ();
void paycompute();
void date_coverage();
//global variables
string E_code,E_name,E_record,E_level,d_coverage;;
double perday,T_Hours,T_Mins;
int main(){
do{
system("cls");
//Accept input from user
cout<<"Enter Employee ID: ";
getline(cin,E_code);
// opens file for input
ifstream inFile("employee.txt",ios::in);
// if input file can not be open enter the fail state
if(!inFile) {
cout << "Can not open input file" << endl;
cout << "The program will terminate" << endl;
system("pause");
exit(0);
}
//search for employee code
do{
getline(inFile,E_record);
int location = E_record.find(E_code);
if(location>0)
{
E_code = E_record.substr(location,8);
E_level = E_record.substr(E_record.length()-1,1);
E_name = E_record.substr(0,location-1);
break;
}
}while(!inFile.eof());
if (E_level =="1") perday = 380;
if (E_level =="2") perday = 450;
if (E_level =="3") perday = 550;
//Display Records
if (E_name==""){
cout<<"Record not Found!\n\n\n";
system("pause");
exit(0);
}
else {
EmployeeInfo ();
input();
date_coverage();
EmployeeInfo ();
paycompute();
cout<<"\n\n";
system("Pause");
}
}while(true);
}//END
//MY USER-DEFINED FUNCTIONS
void EmployeeInfo (){
system("cls");
printstars ();
cout<<"\n\nEmployee Name :\t"<<E_name;
cout<<"\nEmployee Code :\t"<<E_code;
cout<<"\nEmployee Level:\t"<<"Level "<<E_level;
cout<<"\nEmployee Rate :\t"<<"Php"<<perday<<".00/day";
printstars ();
}
void input ()
{
// declare variables --------
int hrs_in[5],hrs_out[5], min_in[5],min_out[5],x,TH_inout[5];
char colon;
string days []={"Monday","Tuesday","Wednesday","Thursday","Friday"};
// accept input -------
cout<<"\n\nEnter the time using the following format(hour:minutes)\n";
for(x=0;x<5;x++){
cout<<"\n\nEnter Log-in for "<<days[x]<<" : ";
cin>>hrs_in[x]>>colon>>min_in[x];
cout<<"Enter Log-out for "<<days[x]<<" : ";
cin>>hrs_out[x]>>colon>>min_out[x];
}
//write data to text files ------------
ofstream out("dtr.txt",ios::out);
out<<E_code;
for(x=0;x<5;x++){
out<<","<<hrs_in[x]<<":"<<min_in[x]<<","<<hrs_out[x]<<":"<<min_out[x];
} // out<<"\n\nTotal Working Hours: "<<T_Hours<<" Hrs";
cout<<"Files successfully written to dtr.txt files!";
cout<<"\n\n";
system("pause");
//compute number of Total Hours and Minutes --------
for(x=0;x<5;x++){
if(hrs_in[x]<8)
{hrs_in[x]=8;
min_in[x]=00;
}
if(hrs_in[x]>=12 && hrs_in[x]<13)
{
hrs_in[x]=13;
min_in[x]=00;
}
if(hrs_in[x]>=12 && hrs_in[x]<=13)
{TH_inout[x] = (hrs_out[x]-hrs_in[x]);}
else
{TH_inout[x] = (hrs_out[x]-hrs_in[x])-1;}
T_Hours =T_Hours+TH_inout[x];
}
}
void printstars (){
cout<<"\n=================================================";
}
void date_coverage(){
system("cls");
printstars();
cout<<"\nEnter Payroll Date of Coverage: ";
getline(cin,d_coverage);
getline(cin,d_coverage);
}
void paycompute(){
cout<<"\n\nDate of Coverage\t: "<<d_coverage;
cout<<"\nTotal Work Hours\t: "<<T_Hours<<".00 Hrs";
cout<<"\nWeekly Regular Salary\t: "<<"Php"<<((T_Hours/8)*perday)<<".00";
cout<<"\n";
printstars ();
cout<<"\n\n";
}
-
Re: C++/C conversion to just C
Quote:
Originally Posted by
Charmmanuel
I just need someone who could help me about converting this C++ TO C FUNCTION.. this codes:
:mad:
Please, never post unformatted code snippets! No one will read it because such a snippet is unreadable!
Didn't you read the answers to your first post here? For example, post#37?