IMG

 
IMG
IMG   IMG
  Welcome to GTAForums! Be sure to check out the Grand Theft Auto V Forum.

You are not registered! (If you are, click here to login) Registering is fast, free and easy and allows you to instantly reply to any topic on GTAForums.
Why wait? Click here to register your own unique username and become part of the ever-growing community!


( Log In | Register | Revalidate Validation E-mail )
Quick Log-In:
  IMG
       
>
  Reply to this topicStart new topicStart Poll

 c++ help

 
The_Sorrow  
Posted: Saturday, May 21 2011, 19:16
Quote Post


Heaven, I'm in heaven(8)
Group Icon
Group: Members
Joined: May 31, 2010

en.gif

XXXXX



Okay, I'm a beginner so you know please give criticism.

Right here's my code

CODE

#include<iostream>
using namespace std;

int main()
{
while (true)
{
int consoleinput;
cout <<"please enter a number:" <<endl;
{
       consoleinput == 100;
       break;
       }
cin >> consoleinput;
cout <<"The number you entered was: " << consoleinput << endl;
cout <<"if you were to quadruple this number your answer would be: " <<consoleinput*4 <<endl;
}
system("pause");
 return 0;
}


Okay so you enter a number and it quadruples the number. I want to add a option to close the app, which is what the line

CODE
consoleinput == 100;


was for, although i've gone wrong. I need to check if the entered number is 100 then it will close, but i cannot figure it out.

Please help icon14.gif thankyou.


Users WebsitePMMSN
  Top
 

 
The_Siggi  
Posted: Saturday, May 21 2011, 19:51
Quote Post


Surgeon
Group Icon
Group: Members
Joined: Jul 5, 2009

ja.gif

XXXXX



you didn't include the actual check
CODE
if (consoleInput == 100)



Also you need to read the input from the console into the var before(!) checking its value. And I added some brackets around the term you want to display.
CODE

#include<iostream>
using namespace std;

int main()
{
   while (true)
   {
        int consoleInput;
        cout <<"please enter a number:" <<endl;
        cin >> consoleInput;
        if ( consoleInput == 100)
             break;
        cout <<"The number you entered was: " << consoleInput << endl;
        cout <<"if you were to quadruple this number your answer would be: " <<  (consoleInput*4) <<endl;
   }
   system("pause");
        return 0;
}


Also please stick to a different form to name your vars:

CODE
consoleInput
provides better readability than
CODE
consoleinput


This post has been edited by The_Siggi on Saturday, May 21 2011, 19:55
PM
  Top
 

 
Swoorup  
Posted: Friday, Sep 16 2011, 07:29
Quote Post


innovator
Group Icon
Group: Members
Joined: Oct 28, 2008

au.gif

XXXXX



Works great, BTW I am also learning C++. But when I enter a string it loops infinitely. Any solution how to handle those errors?
PMMSNYahoo
  Top
 

 
Kweckzilber  
Posted: Saturday, Sep 17 2011, 06:38
Quote Post



Group Icon
Group: Members
Joined: Jun 17, 2011

XXXXX



QUOTE (Swoorup @ Friday, Sep 16 2011, 07:29)
Works great, BTW I am also learning C++. But when I enter a string it loops infinitely. Any solution how to handle those errors?

Can you post your code here for a better understanding of the problem?
PM
  Top
 

 
Swoorup  
Posted: Saturday, Sep 17 2011, 06:39
Quote Post


innovator
Group Icon
Group: Members
Joined: Oct 28, 2008

au.gif

XXXXX



The code is the same as siggi has written. I think its kind of an error but how to handle it.
PMMSNYahoo
  Top
 

 
Kweckzilber  
Posted: Saturday, Sep 17 2011, 06:43
Quote Post



Group Icon
Group: Members
Joined: Jun 17, 2011

XXXXX



That happens when you input a string where an integer is expected. You will have to terminate the program manually by using Ctrl+C.
PM
  Top
 

 
Swoorup  
Posted: Saturday, Sep 17 2011, 06:47
Quote Post


innovator
Group Icon
Group: Members
Joined: Oct 28, 2008

au.gif

XXXXX



How to handle those errors by coding? Sorry I pretty much on VB and I use those error handling terms and all
PMMSNYahoo
  Top
 

 
Kweckzilber  
Posted: Saturday, Sep 17 2011, 06:53
Quote Post



Group Icon
Group: Members
Joined: Jun 17, 2011

XXXXX



The error you just encountered is not exactly one to be handled by coding. And even if it was, exceptions and handling them is a topic that comes in the later sections of learning C++.

You can somewhat solve it by using a string to receive the input and converting it into an int (manually or using the library functions), after checking whether the user has abided by your instructions.
PM
  Top
 

 
Swoorup  
Posted: Saturday, Sep 17 2011, 07:21
Quote Post


innovator
Group Icon
Group: Members
Joined: Oct 28, 2008

au.gif

XXXXX



Ok will do that. I am confused trying to find out the standard header files for math, string and io functions. What do you recommend?
PMMSNYahoo
  Top
 

 
Kweckzilber  
Posted: Saturday, Sep 17 2011, 07:25
Quote Post



Group Icon
Group: Members
Joined: Jun 17, 2011

XXXXX



I would recommend this Wikipedia page.

C traditionally used stdio.h/conio.h for I/O, math.h for math and string.h for string functions. Did you look in the help/documentation of your compiler?
PM
  Top
 

 
K^2  
Posted: Saturday, Sep 17 2011, 07:29
Quote Post


Vidi Vici Veni
Group Icon
Group: Zaibatsu
Joined: Apr 14, 2004

us.gif

Member Award




Using pre-fab string/number input functions in either stdio or iostream is extremely unsafe. It's fine for learning, but you'd never use them in a real program.

As an example, you can do something like this:

CODE
#include <stdio.h>
#include <stdlib.h>

#define BUFF_SIZE 10

int SafeIntegerInput(void)
{
char buff[BUFF_SIZE];
int i;

i=0;
while(1)
{
 buff[i]=getchar();
 if(buff[i]=='\n')break;
 if(i<BUFF_SIZE-1)i++;
}
buff[i]=0;

for(i=0;buff[i];i++)if(buff[i]<'0' || buff[i]>'9')return 0;

sscanf(buff,"%d",&i);
return i;
}

int main(void)
{
int consoleInput;

while(1)
{
 printf("Please enter a number: ");
 consoleInput=SafeIntegerInput();
 if(consoleInput==100)break;

 printf("The number you entered was %d\n",consoleInput);
}

system("pause");
return 0;
}


Yeah, I prefer C-style input. Sue me.

Anyways, try entering text or ridiculously long numbers with that.
PMMSN
  Top
 

 
Swoorup  
Posted: Saturday, Sep 17 2011, 07:29
Quote Post


innovator
Group Icon
Group: Members
Joined: Oct 28, 2008

au.gif

XXXXX



Nop I even dont know which compiler I use. I just used Codeblocks and dev c?
Which compiler is the best for windows? And how to compile it via CMD? And how to know the syntax of io, strings and math functions?

Sorry man, I think I am bothering you by asking too many questions.
PMMSNYahoo
  Top
 

 
K^2  
Posted: Saturday, Sep 17 2011, 07:37
Quote Post


Vidi Vici Veni
Group Icon
Group: Zaibatsu
Joined: Apr 14, 2004

us.gif

Member Award




You are probably using MinGW compiler if you are using either CodeBlocks or Dev-C++. It's a good one to use. Stick with it for now.

To compile from command line, you need to perform two steps. First, compile your .c or .cpp file into .o file. Second, link one or more .o files into an executable.

CODE
g++ -c example.cpp

CODE
gcc -c example2.c

CODE
g++ example.o example2.o -o example.exe


So you'd use gcc.exe to compile .c files, and g++.exe for everything else. The key -c tells compiler to compile only, without linking. If you have just one file, you can take a shortcut.

CODE
g++ example.cpp -o example.exe


It helps if you have directory where your compiler binaries are stored added to path. Otherwise, you'll have to cd to binaries directory and give full path to your .cpp and .o files.
PMMSN
  Top
 

 
Swoorup  
Posted: Saturday, Sep 17 2011, 07:41
Quote Post


innovator
Group Icon
Group: Members
Joined: Oct 28, 2008

au.gif

XXXXX



Oh thank you sir, You are full of wisedom
EDIT:Hmm it is minggw but in the bin directory I did not find gcc.exe exe. I just found these:
Ar.exe
As.exe
Dlltool.exe
Id.exe
nm.exe
ranlib.exe
strip.exe
PMMSNYahoo
  Top
 

 
Kweckzilber  
Posted: Saturday, Sep 17 2011, 07:44
Quote Post



Group Icon
Group: Members
Joined: Jun 17, 2011

XXXXX



Can you compile from the Dev-Cpp UI?
PM
  Top
 

 
Swoorup  
Posted: Saturday, Sep 17 2011, 07:48
Quote Post


innovator
Group Icon
Group: Members
Joined: Oct 28, 2008

au.gif

XXXXX



Yes, I can. Ok I think I found more list of exe's in the bin directory of DEV Cpp.
Is this the one K^2 was talking about :

mingw32-gcc.exe

EDIT: Oh no need, I found it. My search didnot work actually sorry for bothering blush.gif
PMMSNYahoo
  Top
 

 
Kweckzilber  
Posted: Saturday, Sep 17 2011, 07:52
Quote Post



Group Icon
Group: Members
Joined: Jun 17, 2011

XXXXX



Sort of. But there will be an identical copy of that file, named gcc.exe. That is the one K^2 was talking about exactly.
PM
  Top
 

 
Swoorup  
Posted: Saturday, Sep 17 2011, 08:25
Quote Post


innovator
Group Icon
Group: Members
Joined: Oct 28, 2008

au.gif

XXXXX



Ok I assigned the gcc in the user path but i get an error that it didnot find IOstream.h file
CODE

C:\C>gcc main.c -o main.exe
main.c:1:20: iostream: No such file or directory

CODE
#include <iostream>

int main()
{
printf("hello");
}


EDIT:Never mind. I found out that iostream is C++ standard header so we have to use G++ instead of gcc as gcc does not have iostream.h as its standard header


This post has been edited by Swoorup on Saturday, Sep 17 2011, 08:39
PMMSNYahoo
  Top
 

 
K^2  
Posted: Saturday, Sep 17 2011, 09:14
Quote Post


Vidi Vici Veni
Group Icon
Group: Zaibatsu
Joined: Apr 14, 2004

us.gif

Member Award




printf is defined in stdio.h, not iostream.
PMMSN
  Top
 

 

0 User(s) are reading this topic (0 Guests and 0 Anonymous Users)

0 Members:

Topic Options Reply to this topicStart new topicStart Poll
Search topic for posted by (exact match)



 
IMG IMG