Quantcast

Jump to content

» «
Photo

c++ help

18 replies to this topic
ChopTheDog.
  • ChopTheDog.

    War. War never changes.

  • Members
  • Joined: 31 May 2010
  • None

#1

Posted 21 May 2011 - 07:16 PM

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.



TheSiggi
  • TheSiggi

    Surgeon

  • Members
  • Joined: 05 Jul 2009

#2

Posted 21 May 2011 - 07:51 PM Edited by The_Siggi, 21 May 2011 - 07:55 PM.

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

Swoorup
  • Swoorup

    innovator

  • Members
  • Joined: 28 Oct 2008

#3

Posted 16 September 2011 - 07:29 AM

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

Kweckzilber
  • Kweckzilber

  • Members
  • Joined: 17 Jun 2011

#4

Posted 17 September 2011 - 06:38 AM

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?

Swoorup
  • Swoorup

    innovator

  • Members
  • Joined: 28 Oct 2008

#5

Posted 17 September 2011 - 06:39 AM

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

Kweckzilber
  • Kweckzilber

  • Members
  • Joined: 17 Jun 2011

#6

Posted 17 September 2011 - 06:43 AM

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

Swoorup
  • Swoorup

    innovator

  • Members
  • Joined: 28 Oct 2008

#7

Posted 17 September 2011 - 06:47 AM

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

Kweckzilber
  • Kweckzilber

  • Members
  • Joined: 17 Jun 2011

#8

Posted 17 September 2011 - 06:53 AM

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.

Swoorup
  • Swoorup

    innovator

  • Members
  • Joined: 28 Oct 2008

#9

Posted 17 September 2011 - 07:21 AM

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?

Kweckzilber
  • Kweckzilber

  • Members
  • Joined: 17 Jun 2011

#10

Posted 17 September 2011 - 07:25 AM

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?

K^2
  • K^2

    Vidi Vici Veni

  • Moderator
  • Joined: 14 Apr 2004
  • United-States

#11

Posted 17 September 2011 - 07:29 AM

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.

Swoorup
  • Swoorup

    innovator

  • Members
  • Joined: 28 Oct 2008

#12

Posted 17 September 2011 - 07:29 AM

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.

K^2
  • K^2

    Vidi Vici Veni

  • Moderator
  • Joined: 14 Apr 2004
  • United-States

#13

Posted 17 September 2011 - 07:37 AM

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.

Swoorup
  • Swoorup

    innovator

  • Members
  • Joined: 28 Oct 2008

#14

Posted 17 September 2011 - 07:41 AM

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

Kweckzilber
  • Kweckzilber

  • Members
  • Joined: 17 Jun 2011

#15

Posted 17 September 2011 - 07:44 AM

Can you compile from the Dev-Cpp UI?

Swoorup
  • Swoorup

    innovator

  • Members
  • Joined: 28 Oct 2008

#16

Posted 17 September 2011 - 07:48 AM

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

Kweckzilber
  • Kweckzilber

  • Members
  • Joined: 17 Jun 2011

#17

Posted 17 September 2011 - 07:52 AM

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.

Swoorup
  • Swoorup

    innovator

  • Members
  • Joined: 28 Oct 2008

#18

Posted 17 September 2011 - 08:25 AM Edited by Swoorup, 17 September 2011 - 08:39 AM.

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

K^2
  • K^2

    Vidi Vici Veni

  • Moderator
  • Joined: 14 Apr 2004
  • United-States

#19

Posted 17 September 2011 - 09:14 AM

printf is defined in stdio.h, not iostream.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users