Some questions + What compiler to use?

I decided to learn C++, and I bought the book "C++ A Beginner's Guide" (this one: http://www.amazon.co.uk/Beginners-Guide-Second-Edition/dp/0072232153) to learn it, as it's getting some nice reviews and its not that expensive. When I started, everything was going fine, until Some problems started to appear. The solutions are probably simple, but I can't find them :(

1 [SOLVED]) I'm using the IDE Code::Blocks, with the compiler MiniGW. It was all working fine for the beginning of the book, until I had to use the abs() function. Basically, Code::Blocks says it's invalid, although I include #include <cstdlib> in the code. What's wrong there? And is there a better IDE I can use? UPDATE: I reinstalled minigw and codeblocks, though the codeblocks + minigw setup. I will say how it went.

2[UNSOLVED]) MiniGW doesn't seem to work when I'm offline, and at random times. Also, it says that the path to my project is invalid, and ld.exe path is invalid. What's wrong there? And what other compiler do you recommend?

3[SOLVED]) I thought that Code::Blocks was the problem, so I installed Visual Express. After making a new Win32 Console project, there's this code which I don't understand:
1
2
3
4
5
#include "stdafx.h"

int _tmain(int argc, _TCHAR* argv[])
{
}


Normally I thought it's just:
1
2
3
4
#include <ioswitch>
using namespace std;
int main(){
}


Why is that there in Visual Express?
I tried putting this code in, hoping it would work (as the abs() function isn't working in Code::Blocks, and it doesn't):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream> 
#include <cstdlib>  
using namespace std; 
 
int main() 
{ 
  int i; 
  double avg, val; 
 
  avg = 0.0; 
 
  for(i=0; i<5; i++) { 
    cout << "Enter a value: "; 
    cin >> val; 
 
    avg = avg + abs(val); 
  } 
  avg = avg / 5; 
     
 
  cout << "Average of absolute values: " << avg; 
 
  return 0; 
}

Why is that?

Any help is appreciated :D Thanks for taking your time to read this.
Last edited on
> It was all working fine for the beginning of the book, until I had to use the abs() function.
> Basically, Code::Blocks says it's invalid, although I include #include <cstdlib> in the code.
> What's wrong there?

#include <cmath>
http://en.cppreference.com/w/cpp/numeric/math/fabs

<cstdlib> has abs() only for integral types.
http://en.cppreference.com/w/cpp/numeric/math/abs

http://ideone.com/XiXJu2
When you create a project in visual studio I recommend creating an empty project instead of a console application. After you create the project find the solution explorer. It's probably on the left side of your screen. left click source files, highlight add, click new item, click c++ file (.cpp), then give the file a name, and click add. This will give you a blank screen no #include "stdafx.h" to worry about.

Last edited on
Thanks JLBorges and Yanson, but why does the book tell me it's cstdlib?
Last edited on
> but why does the book tell me it's cstdlib?

Don't know. Perhaps the book asked you to read in int values instead of double values.
abs() for integral values are declared in <cstdlib>

This would be fine:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <cstdlib>
using namespace std;

int main()
{
  int i;
  double avg;
  int val ;

  avg = 0.0;
  for(i=0; i<5; i++) {
    cout << "Enter a value: ";
    cin >> val;

    avg = avg + abs(val);
  }
  avg = avg / 5 ;

  cout << "\nAverage of absolute values: " << avg;
}

http://ideone.com/EvqG9n
Topic archived. No new replies allowed.