|
 |
|
|
|
|
|
c++ help
 |
|
 |
| |
K^2  |
Posted: Saturday, Sep 17 2011, 07:29
|
Vidi Vici Veni

Group: Zaibatsu
Joined: Apr 14, 2004



|
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.
|
|
|
|
|
 |
|
 |
 |
|
 |
| |
K^2  |
Posted: Saturday, Sep 17 2011, 07:37
|
Vidi Vici Veni

Group: Zaibatsu
Joined: Apr 14, 2004



|
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++ 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.
|
|
|
|
|
 |
|
 |
 |
|
 |
| |
0 User(s) are reading this topic (0 Guests and 0 Anonymous Users)
0 Members:
Track this topic
Receive email notification when a reply has been made to this topic and you are not active on the board.
Subscribe to this forum
Receive email notification when a new topic is posted in this forum and you are not active on the board.
Download / Print this Topic
Download this topic in different formats or view a printer friendly version.
| |
 |
|
 |
|
|
|
|