c++ basic problems

Status
Not open for further replies.

holy_devil_

Solid State Member
Messages
9
cann anyone give some basic problems in c++.

basic? .... array,string,class,structure......

I bought a book on c++ by Robert Lafore and problems are very few........
c++ programming on the run................................................







can i make a complete game using c++ alone (like quake3 arena):p
 
No, you cannot make a game like Quake 3 arena with C++ alone, as you would need some experience both building game engines, and doing anything associated with graphical parts of a game. OpenGL or DirectX programming would be required to make a game like Quake 3, or any game with 3d or 2d graphics for that matter.

C++ alone will only give you text-based games, and nothing more, nothing less.

Here is a basic problem:
Make an item shop in which the player starts out with a set amount of gold, and can repeatedly purchase items until the gold does not allow to do so, or until it reaches 0. Once it is no longer possible to purchase items or the gold is 0, give some output stating that fact. Also, each time the player spends gold on an item, make it so that the output given updates the gold each time something is purchased so that the player can keep up with how much is left, and how much has been spent.

I'm working on C++ too, fun fun fun!
 
Here is another one for you:

Reverse Polish Calculator

Implement the following stack functions.

empty() //returns true if stack is empty and false otherwise
push(argument) //pushes item argument on the stack
pop() //removes (but does not return) element on top of stack
top() //returns (but does not remove) element on top of stack

One very common use of a stack is found in calculators such as the Hewlett Packard calculators. Calculators that use a stack use reverse polish (or prefix) notation for arithmetic expressions. Reverse polish notation allows expressions to be entered without the use of parentheses. We are used to working with expressions in infix notation where a binary operator is placed between left and right operands. Parentheses are used to specify the order of operations. An infix expression is of the form LBR (left op, binary operator, right op) which corresponds to the postfix expression LRB (left op, right op, binary operator). Consider the following

Infix expression Postfix Expression
(a+b) ab+
(x - y - z) xy-z-
(x-y-z)/(u+v) xy-z-uv+/
(a^2 + b^2)*(m-n) a2^b2^+mn-*

Use ^ for exponentiation

To evaluate a postfix expression, P, you scan from left to right. When you encounter an operand, X, while scanning P, push it on to the evaluation stack, S. When you encounter an operator, B, you pop the topmost operand on the stack into a variable which represents the left operand, L. Finally, perform the operation B on L and R getting expression LBR, and push the result back on to the evaluation stack, S.

Make sure your postfix expression can handle:
- signed numbers
- multiple digit numbers
- floating point numbers

Delimit the items in the string with a blank space. The evaluation will be in two stages.

stage 1: Remove any excess blank spaces and any invalid characters from the string.
stage 2: Evaluate the postfix expression and display the answer.

Example:

Enter a postfix expression: -3.5 (2) * 12.75 +
Converted string: -3.5 2 * 12.75 +
Result: 5.75
 
#include<iostream.h>
#include<conio.h>
#include<process.h>
#include<string.h>
int gold=1000;
void main()
{
clrscr();
int ch,i=0,j; char items[100][20];
while(1)
{
cout<<" WELCOME TO MY SHOP"<<endl;
cout<<"what would you like to buy : "<<endl;
cout<<" 1.knife(50 golds)\n"<<" 2.pistol(200 golds)\n";
cout<<" 3.shotgun(300 golds)\n"<<" 4.assault_rifle(500 golds)\n";
cout<<" 5.machinegun(800 golds)\n";
cout<<" 6.check my balance and collected items\n"<<" 7.exit\n";

cin>>ch;
switch(ch)
{
case 1: gold=gold-50;
if(gold<=0)
{
cout<<"Sorry! You dont have enough gold"<<endl;
gold=gold+50;
break;
}
strcpy(items,"knife");
i++;
break;
case 2: gold=gold-200;
if(gold<0)
{
cout<<"Sorry! You dont have enough gold"<<endl;
gold=gold+200;
break;
}
strcpy(items,"pistol");
i++;
break;
case 3: gold=gold-300;
if(gold<0)
{
cout<<"Sorry! You dont have enough gold"<<endl;
gold=gold+300;
break;
}
strcpy(items,"shotgun");
i++;
break;
case 4: gold=gold-500;
if(gold<0)
{
cout<<"Sorry! You dont have enough gold"<<endl;
gold=gold+500;
break;
}
strcpy(items,"assault_rifle");
i++;
break;
case 5: gold=gold-800;
if(gold<0)
{
cout<<"Sorry! You dont have enough gold"<<endl;
gold=gold+800;
break;
}
strcpy(items,"machinegun");
i++;
break;
case 6: cout<<" your balance and collected items......"<<endl;
cout<<"BALANCE : "<<gold;
cout<<"\tCOLLECTED ITEMS ...."<<endl;
for(j=0;j<=i;j++)
{
cout<<items[j]<<endl;
}
break;
case 7: exit(0);
}
}
}


this is form my point of view what do you think red recon
 
yes i will try it it seems simple...
but if i am going to store the operands as a string then how will the compiler know that this is a number.wait i think i can use structure or union here.so ill post the code today..........
 
Status
Not open for further replies.
Back
Top Bottom