Node Posted May 21, 2011 Share Posted May 21, 2011 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 thankyou. Link to comment Share on other sites More sharing options...
TheSiggi Posted May 21, 2011 Share Posted May 21, 2011 (edited) 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 May 21, 2011 by The_Siggi Link to comment Share on other sites More sharing options...
Swoorup Posted September 16, 2011 Share Posted September 16, 2011 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 More sharing options...
Kweckzilber Posted September 17, 2011 Share Posted September 17, 2011 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 More sharing options...
Swoorup Posted September 17, 2011 Share Posted September 17, 2011 The code is the same as siggi has written. I think its kind of an error but how to handle it. Link to comment Share on other sites More sharing options...
Kweckzilber Posted September 17, 2011 Share Posted September 17, 2011 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 More sharing options...
Swoorup Posted September 17, 2011 Share Posted September 17, 2011 How to handle those errors by coding? Sorry I pretty much on VB and I use those error handling terms and all Link to comment Share on other sites More sharing options...
Kweckzilber Posted September 17, 2011 Share Posted September 17, 2011 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 More sharing options...
Swoorup Posted September 17, 2011 Share Posted September 17, 2011 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 More sharing options...
Kweckzilber Posted September 17, 2011 Share Posted September 17, 2011 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 More sharing options...
K^2 Posted September 17, 2011 Share Posted September 17, 2011 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 More sharing options...
Swoorup Posted September 17, 2011 Share Posted September 17, 2011 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 More sharing options...
K^2 Posted September 17, 2011 Share Posted September 17, 2011 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 More sharing options...
Swoorup Posted September 17, 2011 Share Posted September 17, 2011 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 More sharing options...
Kweckzilber Posted September 17, 2011 Share Posted September 17, 2011 Can you compile from the Dev-Cpp UI? Link to comment Share on other sites More sharing options...
Swoorup Posted September 17, 2011 Share Posted September 17, 2011 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 Link to comment Share on other sites More sharing options...
Kweckzilber Posted September 17, 2011 Share Posted September 17, 2011 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 More sharing options...
Swoorup Posted September 17, 2011 Share Posted September 17, 2011 (edited) 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 September 17, 2011 by Swoorup Link to comment Share on other sites More sharing options...
K^2 Posted September 17, 2011 Share Posted September 17, 2011 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 More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now