unbeatable0 32 Junior Poster in Training

I also had a big problem understanding pointers, but it's not that hard:
pointers are just like any other variables when you use the asterisk (*) before them - which leads to the value in the address they hold.
They're useful because, just how serkansendur said, you can save computer memory when passing them to functions (because you send the whole address and not a copy of the variable), and you can manage the variables' values throught them.
Passing an address to a pointer in a function is similar to sending a reference (with the prefix &) to a function, except that a reference can't get a new address to refer to.

unbeatable0 32 Junior Poster in Training
#include<iostream>
#include<string>

using namespace std;

#define IS_STRING 1
#define IS_CHARACTER 2
#define IS_INTEGER 3
#define IS_FLOAT 4

int main(void)
{
string Input;
cout<<"Input a string/character/number:\n";
cin>>Input;
short int input_type=0;
int x;
bool DotReached=false;
if(Input.length()==1 && (Input[0]>'9' || Input[0]<'0'))
input_type=IS_CHARACTER;
else
  for(x=0;x<Input.length();x++)
  {
  if((Input[x]>'9' || Input[x]<'0') && Input[x]!='.') 
     {input_type=IS_STRING; break;}
   else if(Input[x]=='.')
     {if(!DotReached) DotReached=ture; else
        {input_type=IS_STRING; break;}
     }
  }
// continue from here
}
ninja_gs commented: Good , wat i needed is Got exactly ........ +1
unbeatable0 32 Junior Poster in Training
if name[i]='space' or i=0 or i=namelength then

Why should the last letter be capital?

unbeatable0 32 Junior Poster in Training

Or you can just use tolower and toupper, and not have to hope that the character set matches the rules for ASCII letters. Note that 'A' to 'Z' and 'a' to 'z' being consecutive is not a safe assumption.

Correct me if I'm wrong, but ASCII 1 to 128 is standard and is the same for all computers, unlike 129-255.
Using the following code, you can see it's in the standard range:

for(unsigned char c=10;c<255;c++)cout<<(int)c<<". "<<c<<"\n";
unbeatable0 32 Junior Poster in Training

Switching from capital letter to small letter and vise versa is easy:
The letter is capital if it's between 'A' and 'Z' (inclusive)(ASCII symbols' values are in order) and the letter is small letter if it's between 'a' and 'z' (inclusive).
So, to change a small letter to a capital, you just subtract ('a'-'A' -- the ASCII difference between small and capital. Note that the value of lower-case letters is greater), and to change from capital to lower-case letter you add 'a'-'A'.

Example snippet:

string Input;
cout<<"Input your name\n";
getline(cin, Input);

if(Input[0]>='a' && Input[0]<='z') Input[0]-='a'-'A';
 for(int x=1; x<Input.length(); x++)
 {
 if(Input[x]>='a' && Input[x]<='z' && Input[x-1]==' ') Input[x]-='a'-'A';
 else if(Input[x]>='A' && Input[x]<='Z') Input[x]+='a'-'A';
 }
unbeatable0 32 Junior Poster in Training

Input to a string, then call a function to check whether it the input string has only digits (and '.', '+' and '-' as first only). If it really has only digits, it's a number. If it has a dot, it's a floating-point number, else it's an integer.
If you reach a character that isn't one of the above, it's simply a string. Furthermore, if the string only has one character, it's a char.

After determining what's the user input, you can use the atoi (ASCII to int) or atof (ASCII to float) to convert the string to a number (if the input is a number, of course)

unbeatable0 32 Junior Poster in Training
private:

double rad[b]ui[/b]s;

I think you swapped these two letters

ddanbe commented: great observation! +1
unbeatable0 32 Junior Poster in Training

" This program models a simple calculator which can add, subtract,multiply,divide, power"

I don't see any power operation in the program. If you meant to the ^ operator, it doesn't do power (at least in c++). It's an operator which works on bits. For power you need to implement a function.

unbeatable0 32 Junior Poster in Training

Tell us what compiler/ide you are using?

But the following link would be the obvious choice.

http://gmplib.org/

What's IDE?
I'm using Dev-C++ (version 4.9.9.2 if it helps...)

unbeatable0 32 Junior Poster in Training

I see... so, are there some classes that people made for huge numbers handling?
I like working with big numbers :P

unbeatable0 32 Junior Poster in Training

Just wanted to know... is there any variable in C++ that can hold larger numbers than unsigned long long int , or is it the largest variable?
Thanks in advance :P

unbeatable0 32 Junior Poster in Training

I actually found this site while trying to fix my awful coding so no, I did not have a chance to read the site's stickies and for that, I apologize. I've been trying to figure this out for the past three hours and I know this is pretty simple but I just started on my own last week. I took out the coding for the in/out file as it was just causing a headache(sintax errors).

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

using namespace std;

int main() 
{ 
    srand((unsigned)time(0)); 
    int random_integer; 
    for(int index=0; index<5; index++){ 
        random_integer = (rand()%55)+1; 
        cout << random_integer << endl;
	}
		
		system ("pause");
}

I have my first five random numbers down but the sixth is where I'm confused as it involves a different range 1-42. I'm not sure how to incorporate the in/output coding either. "Starting out with C++" by Tony Gaddis is kind of brief on those functions...

Perhaps add another two lines of code after the 'for' loop?

random_integer = (rand()%42)+1; 
        cout << random_integer << endl;
unbeatable0 32 Junior Poster in Training

Any help on this? If it helps, I want the macro to be a function. For example:

#ifndef _WINDOWS_H
#define test(); #warning <windows.h> needs to be included for this function!
#endif
int main(void)
{
test();
return 0;
}

If it's not possible to do this way, is there another way to do it, which will give a warning instead of the function, in case the function cannot be used?

unbeatable0 32 Junior Poster in Training

Heh, funny one.
I think it's something that's already declared in strcmp.

unbeatable0 32 Junior Poster in Training

I dont know what would I do without u, thanx man but can u provide me with a sample code so I can just replace

http://www.daniweb.com/code/snippet777.html

unbeatable0 32 Junior Poster in Training

Oh well... I was wrong. My method didn't work :(
Please help me :)

unbeatable0 32 Junior Poster in Training

Never mind, I found the answer :P

unbeatable0 32 Junior Poster in Training

Is there a way to #define something as a #warning?
I tried using the following code, but it gives me an error:
'warning' undeclared (first use this function)

This is the part of code where the error appears:

#define something #warning This is not a standard function!

int main(void)
{
something
return 0;
}

Any help on this please?
If it's not possible to perform, please tell :)

Thanks in advance ;)

unbeatable0 32 Junior Poster in Training

I'd suggest that you use the clock() function. Examply of use:

#include <ctime>
#include <iostream>

#define SECONDS number of seconds here..

using namespace std;

int main(void)
{
int guess;
cin>>guess;

if(clock()>SECONDS*CLOCKS_PER_SEC) return 0;

return 0;
}

Note: clock() returns the number of milliseconds passed since the program began, and CLOCKS_PER_SEC is a defined constant, and its value is 1000;

unbeatable0 32 Junior Poster in Training

Remove the long before the float and change %lf to %f . Then it'll work.
Also, it'd be clearer if you'd intialize j when declaring it, and not some lines after it.

unbeatable0 32 Junior Poster in Training

goto is a statement that tells the program to jump to another place in the program, ignoring everything in between.
It's quite dangerous to use, but I don't see any other simple solution in this case. Just create a label before cin.get() and a goto statement when the game is over.
Example of use:

if(GameOver==true) goto end;
else // do something else

end:
cin.get();
unbeatable0 32 Junior Poster in Training

Hii..I added sleep(10000).. but the problem is its is unable to open windows.h..what might be the problem???? could we avoid sleep slep and go for any other method..? pls help me

If it is so.. I'd suggest you use the goto statement which will lead to the cin.get() line in main().

unbeatable0 32 Junior Poster in Training

It works fine for me... but you made the program sleep 10 milliseconds (1/100 the second). You need to put the seconds*1000 inside the parentheses (examples for ten seconds: Sleep(10000) or Sleep(10*CLOCKS_PER_SEC) ).

unbeatable0 32 Junior Poster in Training

I dont know how to add a flag..where should i add the flag..? I dont want the program to get complicated.. would like to make simple.. pls help me

Flag is just like a normal variable, but changes values according to the situation and the program is tracking its value to know what to do.

About the Sleep() function, instead of the "TIME" put a number. The program will sleep for that number*milliseconds and then will continue. The header should be <windows.h>

unbeatable0 32 Junior Poster in Training

Simply declare a variable (name doesn't matter. In this case I'll call it ExitFlag).
At the beginning initialize it to 0, and when the game is over, make its value 1. Furthermore, change the while loop to "while(ExitFlag==0)" or "while(!ExitFlag)"

You can make the flag value anything you like, just remember the values of each situation.
For your convenience, you can define constant variables to help you remember.
I, for example, use the following definitions many times:
#define YES 1
#define NO 0

('true' and 'false' have the same values of my 'YES' and 'NO')

unbeatable0 32 Junior Poster in Training

For me it works fine... except it doesn't show the message - it closes it program immediately. Maybe you better add a Sleep(TIME) in your code instead of cin.get().
Also, you may want to put the following code to show the player how many seconds it took him:
cout<<"It took you "<<(clock()-StartTyping)/CLOCKS_PER_SEC<<" seconds!";

unbeatable0 32 Junior Poster in Training

what is the difference between return 0 and return(0) ?

I don't think there is a difference, except for the way it's written.
About your code, you forgot to put "game::" before the function declaration, and a "{" when you start declaring it.

unbeatable0 32 Junior Poster in Training

well..i didnt get you..! you mean the init_screen() function,right?.. where should i put it?... Am confused..please please help me

He means that you should move the function declaration outside the class declaration.
Instead of:

public:
   
      int num,random_integer,score;
   
      void dispnum();
   
      void getnum();
  
      void result();
  
      void playgame()
  
      {
  
      int i=0;
      
      score=0;
      
      StartTyping=clock();

  
      while(i<=3)
  
      {
  
      dispnum();
  
      getnum();
  
      result();
  
      i++;
  
      }
  
      }
  
      };

do it like that:

public:
   
      int num,random_integer,score;
   
      void dispnum();
   
      void getnum();
  
      void result();
  
      void playgame();
  };

void game::playgame()
      {
  
      int i=0;
      
      score=0;
      
      StartTyping=clock();

  
      while(i<=3)
  
      {
  
      dispnum();
  
      getnum();
  
      result();
  
      i++;
  
      }
  
      }
unbeatable0 32 Junior Poster in Training

You get errors because you didn't write a number a closed brackets after "if((clock()-StartTyping)/CLOCKS_PER_SEC>"
(Example: "if((clock()-StartTyping)/CLOCKS_PER_SEC>5)"

Also, you should put the "StartTyping=clock();" inside the while loop.

clock() returns the time in milliseconds since the program started. CLOCKS_PER_SECOND is a defined constant and its value is 1000 (makes clock()/CLOCKS_PER_SECOND return the seconds since the program started).

unbeatable0 32 Junior Poster in Training

ooops.. one more thing.. while compiling the program i got a warning :- Functions containing while are not expanded inline ..What do is that?:s

For me it doesn't give a warning... I'm using Dev-C++ and it works completely fine.

unbeatable0 32 Junior Poster in Training

hey it worked..! Thank you so much.. but now i have a doubt... with int deleted how is "score" declared?

It's declared here:

class game
   
      {
   
      public:
   
      int num,random_integer,score;
   
      void dispnum();
   
      void getnum();
  
      void result();
  
      void playgame()

and how what about exit(0); ? why isnt it working?... and how can i add a time limit for entering the code.... ?the allowed time should decrease as the game progresses.. and the it should show that "the game is over and your final score is ______ ! or should i add a break; instead of exit(0); ?

Just put a "cin.get();" before the "exit(0);"
You can't put a "break;" simply because the compiler won't recognize it as a loop.
If it doesn't work, you can add the header windows.h and put a "Sleep(2000);"
or something, so that the program will wait some time before shutting down.

and how can i add an infinte while loop [a loop which end only when the user makes a wrong entry or the user quits by pressing "esc" ?

I believe that you should change this line:

while(i<=3)

to

while(1)
unbeatable0 32 Junior Poster in Training

If he's running turbo C++ v3, I don't think he has headers without .h

Didn't know about that...
Thanks for the info.! :)

unbeatable0 32 Junior Poster in Training

You can add a time limit by adding something like:

clock_t StartTyping;

and every time the game runs do:

StartTyping=clock()

Then, before checking if the answer is correct, check if it was on time.
Example:

if((clock()-StartTyping)/CLOCK_PER_SEC>/*number of seconds here*/)
{
cout<<"It took you too long!"<<endl;
cin.get();
exit(0);
}
else
//the rest of your code

EDIT: Just don't forget to add a "#include <ctime>" line in the head :)
On a side note, I'd suggest you use the c++ headers (those without the .h),
like <iostream> instead of <iostream.h> and <cstdlib> instead of <stdlib.h>

unbeatable0 32 Junior Poster in Training

"move your project to a folder that does not contain spaces, such as c:\dev-c++\source"

Thanks! I didn't think it'll make a trouble to create it on the desktop. It works great now!
You deserve a reputation point from me :P
Thanks again!

unbeatable0 32 Junior Poster in Training
# Project: Project2
# Makefile created by Dev-C++ 4.9.9.2

CPP  = g++.exe -D__DEBUG__
CC   = gcc.exe -D__DEBUG__
WINDRES = windres.exe
RES  = 
OBJ  = "C:/Documents\ and\ Settings/U1/Desktop/main.o" $(RES)
LINKOBJ  = "C:/Documents and Settings/U1/Desktop/main.o" $(RES)
LIBS =  -L"C:/Dev-Cpp/lib" C:/Dev-Cpp/lib/libpowrprof.a  -g3 
INCS =  -I"C:/Dev-Cpp/include" 
CXXINCS =  -I"C:/Dev-Cpp/lib/gcc/mingw32/3.4.2/include"  -I"C:/Dev-Cpp/include/c++/3.4.2/backward"  -I"C:/Dev-Cpp/include/c++/3.4.2/mingw32"  -I"C:/Dev-Cpp/include/c++/3.4.2"  -I"C:/Dev-Cpp/include" 
BIN  = Project2.exe
CXXFLAGS = $(CXXINCS)   -g3
CFLAGS = $(INCS)   -g3
RM = rm -f

.PHONY: all all-before all-after clean clean-custom

all: all-before Project2.exe all-after


clean: clean-custom
	${RM} $(OBJ) $(BIN)

$(BIN): $(OBJ)
	$(CPP) $(LINKOBJ) -o "Project2.exe" $(LIBS)

"C:/Documents\ and\ Settings/U1/Desktop/main.o": C:/Documents\ and\ Settings/U1/Desktop/main.cpp
	$(CPP) -c "C:/Documents and Settings/U1/Desktop/main.cpp" -o "C:/Documents and Settings/U1/Desktop/main.o" $(CXXFLAGS)
unbeatable0 32 Junior Poster in Training

This is the file included:
C:/Dev-Cpp/lib/libpowrprof.a
It just won't compile...

unbeatable0 32 Junior Poster in Training
#include<iostream> //cout and cin 
#include<conio.h> //getch()
#include<stdlib.h>
#include<windows.h>
#include <cstdlib>
#include<PowrProf.h> //SetSuspendState()

using namespace std;
int main(int argc, char *argv[])
{
SetSuspendState(true,false,false);
getch();
return 0;
}

The error points line 26 in another file, named Makefile.win
Here's that line:
$(BIN): $(OBJ)

unbeatable0 32 Junior Poster in Training

Now I get this error:
"26 E:\C++_programs\Makefile.win [Build Error] *** target pattern contains no `%'. Stop. "
(after making it a project and including the said file)

unbeatable0 32 Junior Poster in Training

I don't have the option to choose "project options", since it's not in a project.
Is there a way I can do it not in a project?

unbeatable0 32 Junior Poster in Training

Hey there,
I'm trying to use the SetSuspendState() function to hibernate the computer, but I have no idea what parameters I should send to it. The prototype is "BOOLEAN WINAPI SetSuspendState(BOOLEAN, BOOLEAN, BOOLEAN);"
Since I'm a somewhat beginner in c++ programming, I don't really have an idea what BOOLEAN means and what I should send as a BOOLEAN. I tried the "true" and "false", numbers and variables, but none worked.
The compiler error is as follows:
[Linker error] undefined reference to `SetSuspendState@12'
By the way -- I'm using Dev-C++ 4.9.9.2
I checked about SetSuspendState() in DaniWeb, and googled it, but came up with nothing.
Thanks in advance to the helpers!