Jump to content
    1. Welcome to GTAForums!

    1. GTANet.com

    1. GTA Online

      1. Los Santos Drug Wars
      2. Updates
      3. Find Lobbies & Players
      4. Guides & Strategies
      5. Vehicles
      6. Content Creator
      7. Help & Support
    2. Red Dead Online

      1. Blood Money
      2. Frontier Pursuits
      3. Find Lobbies & Outlaws
      4. Help & Support
    3. Crews

    1. Grand Theft Auto Series

      1. Bugs*
      2. St. Andrews Cathedral
    2. GTA VI

    3. GTA V

      1. Guides & Strategies
      2. Help & Support
    4. GTA IV

      1. The Lost and Damned
      2. The Ballad of Gay Tony
      3. Guides & Strategies
      4. Help & Support
    5. GTA San Andreas

      1. Classic GTA SA
      2. Guides & Strategies
      3. Help & Support
    6. GTA Vice City

      1. Classic GTA VC
      2. Guides & Strategies
      3. Help & Support
    7. GTA III

      1. Classic GTA III
      2. Guides & Strategies
      3. Help & Support
    8. Portable Games

      1. GTA Chinatown Wars
      2. GTA Vice City Stories
      3. GTA Liberty City Stories
    9. Top-Down Games

      1. GTA Advance
      2. GTA 2
      3. GTA
    1. Red Dead Redemption 2

      1. PC
      2. Help & Support
    2. Red Dead Redemption

    1. GTA Mods

      1. GTA V
      2. GTA IV
      3. GTA III, VC & SA
      4. Tutorials
    2. Red Dead Mods

      1. Documentation
    3. Mod Showroom

      1. Scripts & Plugins
      2. Maps
      3. Total Conversions
      4. Vehicles
      5. Textures
      6. Characters
      7. Tools
      8. Other
      9. Workshop
    4. Featured Mods

      1. Design Your Own Mission
      2. OpenIV
      3. GTA: Underground
      4. GTA: Liberty City
      5. GTA: State of Liberty
    1. Rockstar Games

    2. Rockstar Collectors

    1. Off-Topic

      1. General Chat
      2. Gaming
      3. Technology
      4. Movies & TV
      5. Music
      6. Sports
      7. Vehicles
    2. Expression

      1. Graphics / Visual Arts
      2. GFX Requests & Tutorials
      3. Writers' Discussion
      4. Debates & Discussion
    1. Announcements

    2. Support

    3. Suggestions

c++ help


Node
 Share

Recommended Posts

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

 

Right here's my 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

 

 

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.

 

 

 

Link to comment
Share on other sites

you didn't include the actual check

 

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.

 

#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:

 

 

consoleInput

provides better readability than

 

consoleinput

 

Edited by The_Siggi
Link to comment
Share on other sites

  • 3 months later...

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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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:

 

 

#include <stdio.h>#include <stdlib.h>#define BUFF_SIZE 10int 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.

Prior to filing a bug against any of my code, please consider this response to common concerns.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

 

 

g++ -c example.cpp

 

 

gcc -c example2.c

 

 

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.

 

 

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.

Prior to filing a bug against any of my code, please consider this response to common concerns.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

 

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

 

 

#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

 

Edited by Swoorup
Link to comment
Share on other sites

printf is defined in stdio.h, not iostream.

Prior to filing a bug against any of my code, please consider this response to common concerns.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • 1 User Currently Viewing
    0 members, 0 Anonymous, 1 Guest

×
×
  • Create New...

Important Information

By using GTAForums.com, you agree to our Terms of Use and Privacy Policy.