Log in

View Full Version : C++ programming help



F9
6th December 2010, 00:26
The better im going at classes, the harder they make the things.I am wondering if i am doing something wrong :lol:
In any way, to the subject now... I have an assigment to deliver for tuesday.I have made most of the programming, but im missing something in the mathematic formula, on how to achieve the result.Also as the class is in greek and he set us the assignment in english i will need some clarification on what few things are:)

Firstly on the requirements i have this : Indentation 10 points
wtf does it mean? :p

To the juicy part now...The problem.(click on the spoil button)


Write a program that calculates and prints the bill for a cellular telephone company. The company offers two types of services: regular and premium. Its rates vary depending on the type of service. The rates are computed as follows:
 Regular service: 10.00€ plus 0.20€ per minute. However, the first 50 minutes are free of charge.
 Premium service: 25.00€ plus:
1. For calls made from 6:00 A.M. to 6:00 P.M., the first 75 minutes are free; charges for over 75 minutes are 0.10€ per minute.
2. For calls made from 6:00 P.M. to 6:00 A.M., the first 100 minutes are free; charges for over 100 minutes are 0.05€ per minute.
Your program should prompt the user to enter a service code (type char) and the number of minutes the service was used. A service code of r or R means regular
service; a service code of p or P means premium service. Treat any other character as an error.
Your program should output the type of service, the number of minutes the telephone service was used, and the amount due from the user.
For the premium service, the customer may be using the service during the day and the night. Therefore, to calculate the bill, you must ask the user to input the number of minutes the service was used during the day and the number of minutes the service was used during the night.

Result examples: (Again spoil)

Test1
Enter service code r (Regular Service) or p (Premium Service): w
Your service code is invalid

Test2
Enter service code r (Regular Service) or p (Premium Service): r
Enter the number of minutes the telephone service is used: 20
Type of Service: Regular
Minutes used: 20.00
Service charges: 10.00 Euros

Test3
Enter service code r (Regular Service) or p (Premium Service): r
Enter the number of minutes the telephone service is used: 100
Type of Service: Regular
Minutes used: 100.00
Service charges: 20.00 Euros

Test4
Enter service code r (Regular Service) or p (Premium Service): P
Enter the number of minutes the telephone service was used during the day: 0
Enter the number of minutes the telephone service was used during the night: 0
Type of Service: Premium
Minutes used: 0.00
Service charges: 25.00 Euros

Test5
Enter service code r (Regular Service) or p (Premium Service): p
Enter the number of minutes the telephone service was used during the day: 100
Enter the number of minutes the telephone service was used during the night: 200
Type of Service: Premium
Minutes used: 300.00
Service charges: 32.50 Euros

So question 1, how do i get the result?I set as constants(use of constants gives 10 points:tt2:) regular and premium starting prices.But then when i need to add the extra minutes i dont seem to find the correct formula.
I used for regular: sum= regular+((minutes-50)*0.2); ( i can of course understand an issue there, when minutes are <50 but cant figure the way around, and surely not a simple way around which i am sure there is one)
and for premium: sum= premium+((day_minutes-75)*0.1)+((night_minutes-100)*0.05);

builded the programme with no mistakes as it says, just wrong results.Hit spoil button for my full program.

#include <iostream>
using namespace std;

int main ()
{
char type;
float sum, minutes, night_minutes, day_minutes;


const float regular= 10.00, premium=25.00;

cout <<"Enter service code r (Regular Service) or p (Premium Service): ";
cin >> type;

switch (type)
{
case 'r':
{

cout << "Enter the number of minutes the telephone service is used: ";
cin >> minutes;

cout << "Type of Service: Regular" <<endl;
cout << "Minutes used: " << minutes <<endl;

sum= regular+((minutes-50)*0.2);
cout << "Service charges: " << sum << " Euros";
break;
}
case 'p':
{
cout << "Enter the number of minutes the telephone service was used during the day: "<< endl;
cin >> day_minutes;

cout << "Enter the number of minutes the telephone service was used during the night: "<< endl;
cin >> night_minutes;

cout << "Type of Service: Premium" <<endl;
cout << "Minutes used: " << minutes <<endl;

sum= premium+((day_minutes-75)*0.1)+((night_minutes-100)*0.05);
cout << "Service charges: " << sum << " Euros";
break;
}
default: cout << "Your service code is invalid";
break;
}
return 0;
}

Also any idea on comments?He says i should add them, allthough i am not sure where, this course is not programming per se, it focuses mainly on making the programme readable, and "good", so we learn to add comments, add spaces and shit like that, but its still a course.

Help please:( Big fat reputation waitting:D

NonServiam
6th December 2010, 01:46
Cant really help you much because I'm currently learning C++ myself.

What your teacher means by "indentation" is organizing your code by using the "Tab" key.
Example:


int main (){
char type;
float sum, minutes, night_minutes, day_minutes;
const float regular= 10.00, premium=25.00;
cout <<"Enter service code r (Regular Service) or p (Premium Service): ";
cin >> type;
switch (type){

case 'r':{


cout << "Enter the number of minutes the telephone service is used: ";
cin >> minutes;

cout << "Type of Service: Regular" <<endl;
cout << "Minutes used: " << minutes <<endl;

sum= regular+((minutes-50)*0.2);
cout << "Service charges: " << sum << " Euros";
break;
}

When indenting, brackets should line up horizontally. Functions within functions for example would be indented.

Comments should be made inline with the functions, to describe what they do
Example:


function main { // This is a single line comment
stuff
stuff
/* This is
a comment
that spans
multiple lines */

F9
6th December 2010, 01:58
Thanks for that, i would loose easy 10 points for not knowing what indentation meant:)

Allthough i am more worried about the 60 points of the program i have yet managed to make work correctly:tt2:

MarxSchmarx
6th December 2010, 06:29
I haven't tried to compile it but since the i/o stuff looks ok you need conditionals, i.e. something like,

# some header file.h

int main (void)
{
int Total = all_minutes;
double calltime[Total]; // Do a read to get the call time info
int i = // time of day;

double ans = determine_price(i, other arguments from above);
return(0);
}

double determine_price(int i, other args from main)
{
double ans = baseprice;
double ans1 = ans;

if (service == regular)
{

if ((calltime[i] > 18) && (calltime[i] < 5)) # Let's say 6 PM to 5 AM
ans += 0.2*frac_late
else
ans += 0.2
print("The contribution to your total bill from calling late at night was %f euros", ans-ans1);
}
if (service == premium)
{
// do arithmetic pertinent to premium etc..
}
}

F9
6th December 2010, 19:30
I have finally came around it, i didnt used your way marxsch cause honestly... i didnt get it :p
i did this:

#include <iostream>
using namespace std;

int main ()
{
char type;
double sum, minutes, night_minutes, day_minutes;


const float regular= 10.00, premium=25.00;

cout <<"Enter service code r (Regular Service) or p (Premium Service): ";
cin >> type;

switch (type)
{
case 'r':
{
cout << "Enter the number of minutes the telephone service is used: ";
cin >> minutes;
if (minutes<=50)
{
cout << "Type of Service: Regular" <<endl;
cout << "Minutes used: " << minutes <<endl;

sum= regular;
cout << "Service charges: " << sum << " Euros";
}
else
{

cout << "Type of Service: Regular" <<endl;
cout << "Minutes used: " << minutes <<endl;

sum= regular+((minutes-50)*0.2);
cout << "Service charges: " << sum << " Euros";
}
break;
}
case 'p':
{
cout << "Enter the number of minutes the telephone service was used during the day: ";
cin >> day_minutes;

if (day_minutes<=75)
{

sum= premium;
}
else
{

sum= premium+((day_minutes-75)*0.1);
}

cout << "Enter the number of minutes the telephone service was used during the night: ";
cin >> night_minutes;

if ((night_minutes<=100) && (day_minutes >75))
{

sum+=premium;

}

else if (day_minutes >=75)
{

sum+=((night_minutes-100)*0.05);

}

cout << "Type of Service: Premium" <<endl;
cout << "Minutes used: " << day_minutes+night_minutes <<endl;
cout << "Service charges: " << sum << " Euros"<<endl;
break;
}
default: cout << "Your service code is invalid";
break;
}
return 0;
}


my test results seem perfect beside one thing.The results that are rounded, dont have .5 or something are shown in integers, where the problem and test results shows them as real numbers(check 2nd spoiler of 1st post).Is there a (simple:p) way of achieving that?
Im just few comments left and this thing and im printing it:)

MarxSchmarx
7th December 2010, 06:01
No problemo, often there's more than one way to code something like this. The way you're doing it is just fine.


I have finally came around it, i didnt used your way marxsch cause honestly... i didnt get it :p
i did this:

#include <iostream>
using namespace std;

int main ()
{
char type;
double sum, minutes, night_minutes, day_minutes;


const float regular= 10.00, premium=25.00;

cout <<"Enter service code r (Regular Service) or p (Premium Service): ";
cin >> type;

switch (type)
{
case 'r':
{
cout << "Enter the number of minutes the telephone service is used: ";
cin >> minutes;
if (minutes<=50)
{
cout << "Type of Service: Regular" <<endl;
cout << "Minutes used: " << minutes <<endl;

sum= regular;
cout << "Service charges: " << sum << " Euros";
}
else
{

cout << "Type of Service: Regular" <<endl;
cout << "Minutes used: " << minutes <<endl;

sum= regular+((minutes-50)*0.2);
cout << "Service charges: " << sum << " Euros";
}
break;
}
case 'p':
{
cout << "Enter the number of minutes the telephone service was used during the day: ";
cin >> day_minutes;

if (day_minutes<=75)
{

sum= premium;
}
else
{

sum= premium+((day_minutes-75)*0.1);
}

cout << "Enter the number of minutes the telephone service was used during the night: ";
cin >> night_minutes;

if ((night_minutes<=100) && (day_minutes >75))
{

sum+=premium;

}

else if (day_minutes >=75)
{

sum+=((night_minutes-100)*0.05);

}

cout << "Type of Service: Premium" <<endl;
cout << "Minutes used: " << day_minutes+night_minutes <<endl;
cout << "Service charges: " << sum << " Euros"<<endl;
break;
}
default: cout << "Your service code is invalid";
break;
}
return 0;
}


my test results seem perfect beside one thing.The results that are rounded, dont have .5 or something are shown in integers, where the problem and test results shows them as real numbers(check 2nd spoiler of 1st post).Is there a (simple:p) way of achieving that?
Im just few comments left and this thing and im printing it:)


printf("Service charges: %f\n Euros", sum);

Die Neue Zeit
7th December 2010, 14:59
All you guys here have a programming background? Is this programming for high school or for uni? :confused:

F9
7th December 2010, 15:44
All you guys here have a programming background? Is this programming for high school or for uni? :confused:

Uni.I took pascal in last year of high school, so yeah pretty much i have some programming backround but nothing big.I just did the basics with pascal and now i am redoing the basics on C++.It was definitely an easier transition now, than when i first learned pascal.

I dont know about what marx wrote, as i said i read them and tried to understood them, but i couldnt, and thats not suprising as i am taking just Programming 1, but mine coding isnt that hard, its just simple selection statements.My main issues was with formulas;) Nothing though that would be hard to know for someone who shows interest.

In any way, thanks all(you 2-3) for the advices, printed and ready to hand in:)

Black Sheep
16th December 2010, 09:37
Did you learn C before C++?

F9
16th December 2010, 11:32
Did you learn C before C++?

Nope.I said i did pascal though which dont has many differences really.

Die Neue Zeit
16th December 2010, 14:52
Methinks C is harder to code in than C++, but I'm sure the C++ both of us had experience with is nothing compared to object-oriented C++. Pascal wasn't that hard, but I was exposed to both Pascal and C++ in high school. Too bad they had to use different programming languages for uni, not to mention a whole course on writing plain algorithms before committing to code!

F9
16th December 2010, 15:06
I have never seen C, i have just used VB, Pascal and now C++.I think my uni right now does only C++ unless i take a course as an extra, not on my main required courses.

Btw, i have been given new assignement and this time i really screwed up because i have no idea what to do.Lecturer gave us a program, and asks from us to detect the problems-mistakes.Mistakes arent syntax errors, they are errors that i need debugging to identify.Sadly i missed that one lecture and i have no clue what to do:( Is there any simple debugging tutorial?

MarxSchmarx
16th December 2010, 20:31
Btw, i have been given new assignement and this time i really screwed up because i have no idea what to do.Lecturer gave us a program, and asks from us to detect the problems-mistakes.Mistakes arent syntax errors, they are errors that i need debugging to identify.Sadly i missed that one lecture and i have no clue what to do:( Is there any simple debugging tutorial?

What compiler are you using?

This should get you started:
http://www.uppmax.uu.se/support/user-guides/short-tutorial-on-using-debuggers-and-profiling
if you are using the common gcc. I think Microsoft visual c++ has a built in debugger as well, tho, so google the compiler

F9
16th December 2010, 21:06
What compiler are you using?

This should get you started:
http://www.uppmax.uu.se/support/user-guides/short-tutorial-on-using-debuggers-and-profiling
if you are using the common gcc. I think Microsoft visual c++ has a built in debugger as well, tho, so google the compiler

i am using eclipse and compiler is tdm-gcc 4.5.1.
Eclipse has a button of debugging for sure, i saw some others doing it, but we were all on the rush of doing stuff so they couldnt explain.I was seeing them putting some blue spots on the right or something and clicking the debug button.Does the link still apply, or is something different?

MarxSchmarx
17th December 2010, 00:21
Hmm yeah I don't know anything about eclipse sorry :(

There's this thing on Java:

WeSitNPAExg

and from the looks of it I'd be surprised if it was all that different for C++. Should get you started.

You can download the rest but it takes almost 2 hours to go through.

F9
17th December 2010, 01:16
weirdly i didnt thought of youtube for videos :p
its the think that we also dont know which one to trust.
Will have a look at them, to see if they are of any help;)

F9
3rd January 2011, 01:49
Back again, and back from time off:D:(

2 projects left from my 1st semester, and obviously as the last ones they are by far the hardest..I only got to start one of them and they are both due to 10 Jan..

Anw my project, to the spoiler:


Math Learning Tool
The purpose of this project is to create a Math Learning Tool. For this
project, you will have to write functions to do each part of the program.
Description
The program will ask the user a series of questions, and will keep track of
the number of good / bad answers. For each question, the computer chooses 2
numbers between 0 and 9 and 1 operator (+, -, *, /). The user has to guess the
result of the operation.

Example of a run (the user input is in blue):

Welcome to "Who is the best in math?"
You will be asked a series of question for which you need to
guess the answer as quickly as possible. Ready?
How many questions would you like to have? 11
3 + 5 = 8
Correct!
2 + 5 = 7
Correct!
3 * 0 = 0
Correct!
8 - 2 = 5
Wrong!
1 * 7 = 7
Correct!
0 + 5 = 5
Correct!
6 / 2 = 2
Wrong!
5 + 9 = 11
Wrong!
5 * 3 = 15
Correct!
7 - 8 = -1
Correct!
11 + 6 = 17
Correct!
Thank you for playing!
You had 72 % of correct answers, and 28 % of wrong answers
You answered in 37 seconds
Your longest streak of correct answers is: 3
and your longest streak of wrong answers is: 2

Features
1. The program should keep track of
(a) the number of correct answers and
(b) the number of wrong answers,
(c) and display the percentage of each at the end of a game.
2. The program should count the time need to answer all the questions.
Hint: to calculate the time, save time(0) in a variable at the beginning
and compare it with time(0) at the end of your program.
3. The program should increase the diculty of the questions after every 10
questions in the following way:
 for questions 11-20: Increase the range of possible numbers by 10.
So the possible numbers will be between 0 and 19;
 for questions 21-30: Increase the number of operators to 2.
Example: The program can ask for the result of 2 + 3 * 5
 for questions 31-40: Increase the range of possible numbers by 10.
So the possible numbers will be between 0 and 29;
 for questions 41-50: Increase the number of operators to 3.
Example: The program can ask for the result of 15 - 3 * 25 + 1
4. The program should display, separately, the longest streak, i.e.
(a) the maximum number of correct answers in a row
(b) and the maximum number of wrong answers in a row


i started thinking on how the hell i would get random operator.i have thought the way around(array) but the outcome is still not the one needed.It gets me an operator, but every time i run it, its the same...
The programing i have done on this, on the spoiler:


#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;


char oper ()
{
char op;
char operators[] = {'+','-','*','/','\0'};


op=operators[rand() % 5];
return op;

}
int main ()
{
char o;
o= oper();
cout << o;
system ("pause");
return 0;
}


I tried using seed, it changes the operator, but still its the same every time.The examples i had were always having the user adding a value to seed, but i initialize it to 1, (cause as per example user dont inputs seed) and i was getting one operator, then make it 0 and get another one etc etc.Anyone with an idea? :( its not an easy task this project, is by far the hardest think i have ever saw in programming.. I also abandoned eclipse cause i was close on punching the pc, and downloaded MV C++ 2010, cause eclipse with that program above was giving me 1 as operator:rolleyes:

F9
3rd January 2011, 16:48
and yes, the way around has been reached :thumbup1: but once again, another problem comes up in the surface.As its a math tool, it needs to make an equation and 2 at least numbers but my beyond programming gives me the same 2 number each and every time..Why is that?It gets storaged?How can i make it use random again the second time i call the function again?any ideas?


#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

void num ()
{
srand(time(0));
cout << rand() % 10;
}

void oper ()
{
char operators[] = {'+','-','*','/'};

srand(time(0));
cout << operators[rand() % 4];
}
int main ()
{

num();
oper();
num();

system ("pause");
return 0;
}

Kamerat
3rd January 2011, 18:03
Try just executing srand just once.
Since srand seeds a random sequence generated by rand() it will start over again on the same sequence every time srand is called, that is if srand is called on the same second. Which it is in your program since its so small.

Hope that helps.

F9
3rd January 2011, 18:38
how do i execute srand just once? also, the program will become bigger, this is just a test to see if my functions are working correctly(you can see the whole problem in post #17, and first spoiler)

Kamerat
3rd January 2011, 19:50
Execute srand just once like so:

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
void num ()
{
cout << rand() % 10;
}
void oper ()
{
char operators[] = {'+','-','*','/'};
cout << operators[rand() % 4];
}
int main ()
{
srand(time(0));
num();
oper();
num();
system ("pause");
return 0;
}The complete solution:


bool mathQuestion ()
{
int firstNumber = rand() % 10;
int operatorNumber = rand() % 4;
int lastNumber = rand() % 10;
int answer;
switch (operatorNumber)
{
case 0:
answer = firstNumber + lastNumber;
break;
case 1:
answer = firstNumber - lastNumber;
break;
case 2:
answer = firstNumber * lastNumber;
break;
default:
answer = firstNumber / lastNumber;
}
char operators[] = {'+','-','*','/'};
cout << firstNumber << " " << operators[operatorNumber] << " ";
cout << lastNumber << endl;
int userAnswer;
cin >> userAnswer;
if (answer == userAnswer)
{
return true;
}
return false;
}
int main ()
{
srand(time(0));
int i;
int points;
cout << "How many questions would you like to have?";
cin >> i;
for (int n = 0; n < i; n++;)
{
if (mathQuestion ())
{
points++;
}
}
cout << "Your points: " << points << endl;
return 0;
}Note that i have writen all code inside revlefts editor and have not compiled it. It could be some mistakes but the algorithms are fine i think.
Also note that i have not programmed c++ in a while, just Java and C#.

F9
4th January 2011, 03:27
thanks for that, however i wont see the solution, i will try to make it on my own, and unless i find myself short of the deadline will have a look at it;)
i still have some issues with my program, maybe i will come back again:D

F9
6th January 2011, 16:37
ok new assignment on the table, and this time things look worse for me.Its probably the first time i look at a problem and not knowing or even imaging what i should do to get this done:( weird problem... click on spoiler for any possible contribution i would really appreciate cause frankly as noted i am completely losed on this particular assignment.


Write a C++ program to display the function
y = x2
in the range minX£ x £ maxX and minY£ y £ maxY, where minX, maxX, minY and maxY are constants. The xy-axis
should also be displayed.
You are adviced to use the following constant declarations:
/* The computer screen has 79 columns and 24 rows. */
const int screen_length = 79;
const int screen_height = 24;
const int minx = -10;
const int maxx = 10;
const int miny = -10;
const int maxy = 100;
If the values of minX, maxX, minY and maxY are as above the graph in Fig.1 will be generated. However, if I change
the values of these contstants, your program should generate the appropriate graph, i.e. if
const int minx = -5;
const int maxx = 30;
const int miny = -50;
const int maxy = 500;


figures

http://img64.imageshack.us/img64/1153/figs.png (http://img64.imageshack.us/i/figs.png/)

Uploaded with ImageShack.us (http://imageshack.us)

F9
7th January 2011, 16:26
It is as tough as i thought it is?I am not the only one not having a clue on what it is? :p

Black Sheep
8th January 2011, 11:48
At first glance:
create a 24 x 79 matrix

since there are limits of x's and y's range, create a block or function to check and calculate the functional limits of the graph, in relation with the matrix

try to find a function that will map every element of the matrix to 0 or 1 (line point or not).My guess is that you'll have to use maxaxis,minaxis,axislength (where axislength is 79 or 24, the others relative to the input).

I ll work on it a bit,and will update if i come up with sth.

Black Sheep
8th January 2011, 12:09
I think the hard part is to design the scaling.
For example in the x (y) axis, if maxX-minX (maxY-minY) is 79 (23) , then every column of your array will correspond to ONE value of x (every row will correspond to ONE value of y)

If maxX-minX > 79, then you'll have to decide which and how many neighbouring graph points (array cells) will also be a part of the graph, scaling it horizontally.

F9
8th January 2011, 23:51
Matrix is a 2 dimensional array? i have to admit i am hardly getting what you told me :p

Black Sheep
9th January 2011, 10:52
Yup, linear algebra stuff.Maybe i am mixing together the terms.
A 24x79 2D array, to represent the "screen" on which the graph will be printed.
(24 rows and 79 columns, as that is the screen's range,given by the problem statement)

btw your teacher must be a dumbass,since he's telling you that the minX,minY etc can be changed, and the graph should scale accordingly, and he's told you to declare them as const ints.



Ask away about the things you don't get,understanding makes programming 0% boring.
Okay maybe like 10%.
Feel free to mail me as well.

F9
9th January 2011, 14:15
ok, a nice (green) comrade helped me last night, actually tbh she did it completely.:D It went against from my normal approach on programming but i still find this problem too fucking tricky!Anw, the problem still has some issues, it was 6 am so she couldnt finish it, so if any of you have any corrections feel free else i will hand it like this, at least i have something to give:) Program as a kind offer from a comrade:


#include <iostream>
#include <math.h>
using namespace std;
int fn(double x, double y){
if (y==pow(x,2)) return 1;
return 0;
}
void main(){
/* The computer screen has 79 columns and 24 rows. */
const double screen_length = 79;
const double screen_height = 24;
const double minx = -10;
const double maxx = 10;
const double miny = -10;
const double maxy = 100;
double stepx = (maxx - minx)/screen_length;
double stepy = (maxy - miny)/screen_height;
for (double y=maxy; y>=miny; y-=stepy){
for (double x=minx; x<=maxx; x+=stepx){
if (fn(x,y)) cout<<"*";
else if (y>pow(x,2) && ((y-stepy)<pow(x,2)) && ((y-pow(x,2)) < (pow(x,2)-(y-stepy)))) cout<<"*";
else if (y<pow(x,2) && ((pow(x,2)-y) < ((y-stepy)-pow(x,2)))) cout<<"*";
else if (x==0 || (x<0 && x+stepx>0)) cout<<"|";
else if (y==0 || (y>0 && y-stepy<0)) cout<<"-";
else cout<<".";
}

}
system ("pause");
}


comrade here seemed to followed your lead of 2d array bs ;p

F9
11th January 2011, 17:44
Back on my first assignment(due to tommorow, i just delivered the above few hours ago...)


#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int num ()
{
int n1;

n1= rand() % 10;
cout << n1;
return n1;
}

int num2 ()
{
int n2;

n2= rand() % 20;
cout << n2;
return n2;
}

int num3 ()
{
int n3;

n3= rand() % 30;
cout << n3;
return n3;
}

char oper ()
{
char operators[] = {'+','-','*','/'};
char op;

op= operators[rand() % 4];
cout <<op;
return op;
}
int main ()
{
double start,end;
int ans,questions,i,j,n1,n2,correct,wrong;
char op;
srand(time(0));

cout << "Welcome to \"Who is the best in math?\""<< endl;
cout << "You will be asked a series of question for which you need to guess the answer as quickly as possible. Ready? " <<endl;
cout << "How many questions would you like to have? ";
cin >> questions;

correct=0;
wrong=0;

start=time(0);
if (questions<=10)
{
for (i=0; i<questions; i++)
{
n1=num();
op=oper();
n2=num();
cout << "= ";
cin >> ans;
/* result=n1 op n2;
if ((result)== ans)
{
cout << "Correct!";
correct++;
}
else
{
cout << "Wrong!";
wrong++;
}

*/ }
}


end=time(0);
cout << "You answered in " << end-start << " seconds";

system ("pause");
return 0;
}


i obviously have an issue getting the result, i mean how can i make the character act as a math sign?I saw kamerats solution but dont seems viable when i will need to make possible numbers up to 20 and 30, and when i make the signs 3 it gets even longer.So question is, is kamerats solution the only way to go, or is there a way around with my "edition" to make that happen?

F9
11th January 2011, 18:52
On second view i cant see how the kamerats edition will work either when it will come to 3 operators,with 2 seems really plausible, i dont know about 3.Anyway, any hints on my program(spoiler) above?

F9
12th January 2011, 08:46
anyone? i am few hours before deadline:(

F9
12th January 2011, 12:54
i guess a mistaken working program is better at this case than a correct not working one so...

#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int num ()
{
int n1;

n1= rand() % 10;
cout << n1;
return n1;
}

int num2 ()
{
int n2;

n2= rand() % 20;
cout << n2;
return n2;
}

int num3 ()
{
int n3;

n3= rand() % 30;
cout << n3;
return n3;
}

char oper ()
{
char operators[] = {'+','-','*','/'};
char op;

op= operators[rand() % 4];
cout <<op;
return op;
}

char oper2 ()
{
char operators[] = {'+','-','*','/'};
char op2;

op2= operators[rand() % 4];
cout <<op2;
return op2;
}

char oper3 ()
{
char operators[] = {'+','-','*','/'};
char op3;

op3= operators[rand() % 4];
cout <<op3;
return op3;
}

int result1(int n1, char op, int n2)
{
int answer;
switch (op)
{
case '+':
answer = n1 + n2;
break;
case '-':
answer = n1 - n2;
break;
case '*':
answer = n1 * n2;
break;
case '/':
answer = n1 / n2;
}
return answer;
}

int result2(int n2, char op2, int n3)
{
int answer;
switch (op2)
{
case '+':
answer = n2 + n3;
break;
case '-':
answer = n2- n3;
break;
case '*':
answer = n2 * n3;
break;
case '/':
answer = n2 / n3;
}
return answer;
}

int result3(int n3, char op3, int n4)
{
int answer;
switch (op3)
{
case '+':
answer = n3 + n4;
break;
case '-':
answer = n3- n4;
break;
case '*':
answer = n3 * n4;
break;
case '/':
answer = n3 / n4;
}
return answer;
}

int main ()
{
double start,end;
bool fcorr,fwrong;
int ans,i,n1,n2,n3,n4,correct,wrong,scorr,swrong;
unsigned short int questions;
char op,op2,op3;
srand(time(0));

cout << "Welcome to \"Who is the best in math?\""<< endl;
cout << "You will be asked a series of question for which you need to guess the answer as quickly as possible. Ready? " <<endl;
cout << "How many questions would you like to have? ";
cin >> questions;

correct=0;
wrong=0;
scorr=0;
fcorr=true;
swrong=0;
fwrong= true;

start=time(0);
if (questions<=10)
{
for (i=0; i<questions; i++)
{
n1=num();
op=oper();
n2=num();
cout << "= ";
cin >> ans;

if ((result1(n1,op,n2))== ans)
{
cout << "Correct!"<<endl;
correct++;
if(fcorr)
scorr++;
else
fcorr= false;
}
else
{
cout << "Wrong!"<<endl;
wrong++;
if(fwrong)
swrong++;
else
fwrong= false;
}
}
}
else if (questions <=20)
{
for (i=0; i<10; i++)
{
n1=num();
op=oper();
n2=num();
cout << "= ";
cin >> ans;

if ((result1(n1,op,n2))== ans)
{
cout << "Correct!"<<endl;
correct++;
if(fcorr)
scorr++;
else
fcorr= false;
}
else
{
cout << "Wrong!"<<endl;
wrong++;
if(fwrong)
swrong++;
else
fwrong= false;
}

}
for (i=10; i<questions; i++)
{
n1=num2();
op=oper();
n2=num2();
cout << "= ";
cin >> ans;

if ((result1(n1,op,n2))== ans)
{
cout << "Correct!"<<endl;
correct++;
if(fcorr)
scorr++;
else
fcorr= false;
}
else
{
cout << "Wrong!"<<endl;
wrong++;
if(fwrong)
swrong++;
else
fwrong= false;
}
}
}
else if (questions <=30)
{
for (i=0; i<10; i++)
{
n1=num();
op=oper();
n2=num();
cout << "= ";
cin >> ans;

if ((result1(n1,op,n2))== ans)
{
cout << "Correct!"<<endl;
correct++;
if(fcorr)
scorr++;
else
fcorr= false;
}
else
{
cout << "Wrong!"<<endl;
wrong++;
if(fwrong)
swrong++;
else
fwrong= false;
}

}
for (i=10; i<20; i++)
{
n1=num2();
op=oper();
n2=num2();
cout << "= ";
cin >> ans;

if ((result1(n1,op,n2))== ans)
{
cout << "Correct!"<<endl;
correct++;
if(fcorr)
scorr++;
else
fcorr= false;
}
else
{
cout << "Wrong!"<<endl;
wrong++;
if(fwrong)
swrong++;
else
fwrong= false;
}
}
for (i=20; i<questions; i++)
{
n1=num2();
op=oper();
n2=num2();
op2=oper2();
n3=num2();
cout << "= ";
cin >> ans;

if ((result2(result1(n1,op,n2),op2,n3))== ans)
{
cout << "Correct!"<<endl;
correct++;
if(fcorr)
scorr++;
else
fcorr= false;
}
else
{
cout << "Wrong!"<<endl;
wrong++;
if(fwrong)
swrong++;
else
fwrong= false;
}
}
}
else if (questions <=40)
{
for (i=0; i<10; i++)
{
n1=num();
op=oper();
n2=num();
cout << "= ";
cin >> ans;

if ((result1(n1,op,n2))== ans)
{
cout << "Correct!"<<endl;
correct++;
if(fcorr)
scorr++;
else
fcorr= false;
}
else
{
cout << "Wrong!"<<endl;
wrong++;
if(fwrong)
swrong++;
else
fwrong= false;
}

}
for (i=10; i<20; i++)
{
n1=num2();
op=oper();
n2=num2();
cout << "= ";
cin >> ans;

if ((result1(n1,op,n2))== ans)
{
cout << "Correct!"<<endl;
correct++;
if(fcorr)
scorr++;
else
fcorr= false;
}
else
{
cout << "Wrong!"<<endl;
wrong++;
if(fwrong)
swrong++;
else
fwrong= false;
}
}
for (i=20; i<30; i++)
{
n1=num2();
op=oper();
n2=num2();
op2=oper2();
n3=num2();
cout << "= ";
cin >> ans;

if ((result2(result1(n1,op,n2),op2,n3))== ans)
{
cout << "Correct!"<<endl;
correct++;
if(fcorr)
scorr++;
else
fcorr= false;
}
else
{
cout << "Wrong!"<<endl;
wrong++;
if(fwrong)
swrong++;
else
fwrong= false;
}
}
for (i=30; i<questions; i++)
{
n1=num3();
op=oper();
n2=num3();
op2=oper2();
n3=num3();
cout << "= ";
cin >> ans;

if ((result2(result1(n1,op,n2),op2,n3))== ans)
{
cout << "Correct!"<<endl;
correct++;
if(fcorr)
scorr++;
else
fcorr= false;
}
else
{
cout << "Wrong!"<<endl;
wrong++;
if(fwrong)
swrong++;
else
fwrong= false;
}
}
}
else if (questions <=50)
{
for (i=0; i<10; i++)
{
n1=num();
op=oper();
n2=num();
cout << "= ";
cin >> ans;

if ((result1(n1,op,n2))== ans)
{
cout << "Correct!"<<endl;
correct++;
if(fcorr)
scorr++;
else
fcorr= false;
}
else
{
cout << "Wrong!"<<endl;
wrong++;
if(fwrong)
swrong++;
else
fwrong= false;
}

}
for (i=10; i<20; i++)
{
n1=num2();
op=oper();
n2=num2();
cout << "= ";
cin >> ans;

if ((result1(n1,op,n2))== ans)
{
cout << "Correct!"<<endl;
correct++;
if(fcorr)
scorr++;
else
fcorr= false;
}
else
{
cout << "Wrong!"<<endl;
wrong++;
if(fwrong)
swrong++;
else
fwrong= false;
}
}
for (i=20; i<30; i++)
{
n1=num2();
op=oper();
n2=num2();
op2=oper2();
n3=num2();
cout << "= ";
cin >> ans;

if ((result2(result1(n1,op,n2),op2,n3))== ans)
{
cout << "Correct!"<<endl;
correct++;
if(fcorr)
scorr++;
else
fcorr= false;
}
else
{
cout << "Wrong!"<<endl;
wrong++;
if(fwrong)
swrong++;
else
fwrong= false;
}
}
for (i=30; i<40; i++)
{
n1=num3();
op=oper();
n2=num3();
op2=oper2();
n3=num3();
cout << "= ";
cin >> ans;

if ((result2(result1(n1,op,n2),op2,n3))== ans)
{
cout << "Correct!"<<endl;
correct++;
if(fcorr)
scorr++;
else
fcorr= false;
}
else
{
cout << "Wrong!"<<endl;
wrong++;
if(fwrong)
swrong++;
else
fwrong= false;
}
}
for (i=40; i<questions; i++)
{
n1=num3();
op=oper();
n2=num3();
op2=oper2();
n3=num3();
op3=oper3();
n4=num3();
cout << "= ";
cin >> ans;

if ((result3(result2(result1(n1,op,n2),op2,n3),op3,n4 ))== ans)
{
cout << "Correct!"<<endl;
correct++;
if(fcorr)
scorr++;
else
fcorr= false;
}
else
{
cout << "Wrong!"<<endl;
wrong++;
if(fwrong)
swrong++;
else
fwrong= false;
}
}
}


end=time(0);
cout<< "Thank you for playing!"<<endl;
cout << "You had " << static_cast<float>(correct)/questions*100 << " % of correct answers, and " << static_cast<float>(wrong)/questions*100 << " % of wrong answers"<<endl;
cout << "You answered in " << end-start << " seconds"<<endl;
cout << "Your longest streak of correct answers is: " <<scorr <<endl;
cout << "and your longest streak of wrong answers is: " <<swrong <<endl;
system ("pause");
return 0;
}


i am pretty sure that wasnt the easiest way, but it was the only way i found out, cause beside kamerats post i searched web for an hour or so and nothing else i found.A mistake is generated from the math tool when it comes to multiple operators cause equation happens based on order of numbers and not priority(*/+-) so 5+2*5=25(???) instead of 15.So yeah not a great math tool, but what to do? :p better than nothing heh

F9
19th March 2011, 02:49
i was "absent" for few months, but new semester new troubles:lol: i am pretty sattisfied with the job i managed to do on my last assignment however there are some parts missing, and i am not at all sure how to achieve them.See i am writting a program that gets a string, makes every letter to lower case and makes anything other than numbers and letters to space char, then i cound how many a's,b's, x's etc etc are in the string, and output them, the total number of characters, and lastly my "issue" find and output how many words and how many lines there are in the program.THe program only stops to fullstop, so an enter is changing a line.How do i turn that enter to make me understand that there was a change of line, and how the hell am i getting the words:confused:
Anw, most of my program is pretty much done, i am again not sure if this was the easiest way, if there are any ideas on simplifications also, shot away;)
Here is my code in the spoilers

#include <iostream>
#include <cstring>
#include <stdlib.h>
using namespace std;

const int MAX=500;


int main()
{
char c;
char string[MAX];
int ca=0,cb=0,cc=0,cd=0,ce=0,cf=0,cg=0,ch=0,ci=0,cj=0, ck=0,cl=0,cm=0,cn=0,co=0,cp=0,cq=0,cr=0,cs=0,ct=0, cu=0,cv=0,cw=0,cx=0,cy=0,cz=0;

cout<< "Enter some text followed by a full stop as the first character on a line"<<endl;

int i=0;
while (c=cin.get(), i< MAX-1 && c!='.')
{
if (isupper(c))
c=c+32;
else if(c<48 || (c>57 && c<65) || (c>90 && c<97) || c>123)
c=' ';
string[i++] = c;



switch (c)
{
case 'a': ca++; break;
case 'b': cb++; break;
case 'c': cc++; break;
case 'd': cd++; break;
case 'e': ce++; break;
case 'f': cf++; break;
case 'g': cg++; break;
case 'h': ch++; break;
case 'i': ci++; break;
case 'j': cj++; break;
case 'k': ck++; break;
case 'l': cl++; break;
case 'm': cm++; break;
case 'n': cn++; break;
case 'o': co++; break;
case 'p': cp++; break;
case 'q': cq++; break;
case 'r': cr++; break;
case 's': cs++; break;
case 't': ct++; break;
case 'u': cu++; break;
case 'v': cv++; break;
case 'w': cw++; break;
case 'x': cx++; break;
case 'y': cy++; break;
case 'z': cz++; break;
}
}
string[i] = '\0';

cout << string <<endl;

cout << "a=" <<ca;
cout << " b=" <<cb;
cout << " c=" <<cc;
cout << " d=" <<cd<<endl;
cout << "e=" <<ce;
cout << " f=" <<cf;
cout << " g=" <<cg;
cout << " h=" <<ch<<endl;
cout << "i=" <<ci;
cout << " j=" <<cj;
cout << " k=" <<ck;
cout << " l=" <<cl<<endl;
cout << "m=" <<cm;
cout << " n=" <<cn;
cout << " o=" <<co;
cout << " p=" <<cp<<endl;
cout << "q=" <<cq;
cout << " r=" <<cr;
cout << " s=" <<cs;
cout << " t=" <<ct<<endl;
cout << "u=" <<cu;
cout << " v=" <<cv;
cout << " w=" <<cw;
cout << " x=" <<cx<<endl;
cout << "y=" <<cz;
cout << " z=" <<cz<<endl;

cout << "\nCharacters = " << strlen(string);

system ("pause");

return 0;
}

F9
21st March 2011, 20:27
cough?:(
i know you got bored of me, but i still love you all:wub::wub:

Tjis
21st March 2011, 21:18
A nice trick you can use is that characters are just internally represented as numbers. For example, 'a' is 97. So instead of all those variables, you can do this:


char[26] letters;
char c;
int count = 0;

while ((c=cin.get()) != '.') {
count++;
c = tolower(c);

int index = c - 'a';
letters[index]++;
}

for (int i = 0;i < 26;i++) {
char c = i + 'a';
cout << c << "=" << letters[i] << endl;
}

cout << "\nCharacters = " << count << endl;

So basically use an array to store your letter counts and then loop through that array later on to print the results.
This snippet doesn't do the other requirements though, and will probably crash too if you type anything but a letter or a '.', but hopefully it'll help you somewhat.

To find out how many words were typed, simply keep track of when a whitespace or punctuation is typed right after an alphanumeric character was typed.

checking for a newline is as simple as comparing c with '\n'.

I hope this helps.

F9
22nd March 2011, 02:41
woohoo cheers tjis, i now completed the last 2 missing parts, and i couldnt even imagine how simple they are...:thumbup1:


#include <iostream>
#include <cstring>
#include <stdlib.h>
using namespace std;

const int MAX=500;//array capacity


int main()
{
char c;
char string[MAX];
int cnta=0,cntb=0,cntc=0,cntd=0,cnte=0,cntf=0,cntg=0,c nth=0,cnti=0,cntj=0,cntk=0,cntl=0;
int cntm=0,cntn=0,cnto=0,cntp=0,cntq=0,cntr=0,cnts=0,c ntt=0,cntu=0,cntv=0,cntw=0,cntx=0;
int cnty=0,cntz=0; //counters of a-z characters initialized
int cntlines=0;// initializing counter of lines
int cntwords=0;// initializing counter of words

cout<< "Enter some text followed by a full stop as the first character on a line"<<endl;

int i=0; //loop initialization
while (c=cin.get(), i< MAX-1 && c!='.')
{
if (((c==' ')&&((c+1)!=' '))||(c=='\n'))//checks if 1 alone space char or a change of line which indicates end of a word
cntwords++;//add 1 if sattisfied
if (c=='\n')
cntlines++;//check for new line command and add 1 if true
if (isupper(c))
c=c+32;//add 32 to the character to turn it to lower case if upper
else if(c<48 || (c>57 && c<65) || (c>90 && c<97) || c>123)
c=' ';//turn invalid characters to space character
string[i++] = c;

switch (c)
{
case 'a': cnta++; break;
case 'b': cntb++; break;
case 'c': cntc++; break;
case 'd': cntd++; break;
case 'e': cnte++; break;
case 'f': cntf++; break;
case 'g': cntg++; break;
case 'h': cnth++; break;
case 'i': cnti++; break;
case 'j': cntj++; break;
case 'k': cntk++; break;
case 'l': cntl++; break;
case 'm': cntm++; break;
case 'n': cntn++; break;
case 'o': cnto++; break;
case 'p': cntp++; break;
case 'q': cntq++; break;
case 'r': cntr++; break;
case 's': cnts++; break;
case 't': cntt++; break;
case 'u': cntu++; break;
case 'v': cntv++; break;
case 'w': cntw++; break;
case 'x': cntx++; break;
case 'y': cnty++; break;
case 'z': cntz++; break;// calculation of the sum of characters a-z
}
}
string[i] = '\0';//entering the null character to the end of the string

cout << string <<endl;

cout << "a=" <<cnta;
cout << " b=" <<cntb;
cout << " c=" <<cntc;
cout << " d=" <<cntd<<endl;
cout << "e=" <<cnte;
cout << " f=" <<cntf;
cout << " g=" <<cntg;
cout << " h=" <<cnth<<endl;
cout << "i=" <<cnti;
cout << " j=" <<cntj;
cout << " k=" <<cntk;
cout << " l=" <<cntl<<endl;
cout << "m=" <<cntm;
cout << " n=" <<cntn;
cout << " o=" <<cnto;
cout << " p=" <<cntp<<endl;
cout << "q=" <<cntq;
cout << " r=" <<cntr;
cout << " s=" <<cnts;
cout << " t=" <<cntt<<endl;
cout << "u=" <<cntu;
cout << " v=" <<cntv;
cout << " w=" <<cntw;
cout << " x=" <<cntx<<endl;
cout << "y=" <<cntz;
cout << " z=" <<cntz;//output sum of characters a-z

cout << "\nCharacters = " << strlen(string);// size of array, and the inputed characters
cout << " , Lines = " << cntlines;// number of lines inserted
cout << " , Words = " <<cntwords;//number of words inserted

system ("pause");

return 0;
}