Search Results

Search found 1031 results on 42 pages for 'iostream'.

Page 12/42 | < Previous Page | 8 9 10 11 12 13 14 15 16 17 18 19  | Next Page >

  • What does the C++ compiler error "looks like a function definition, but there is no parameter list;"

    - by SkyBoxer
    #include <iostream> #include <fstream> using namespace std; int main { int num1, num2; ifstream infile; ostream outfile; infile.open("input.dat"); outfile.open("output.dat"); infile >> num 1 >> num 2; outfile << "Sum = " << num1 + num2 << endl; infile.close() outfile.close() return 0; } This is what I did and when I compile it, I got this error that said error C2470: 'main' : looks like a function definition, but there is no parameter list; skipping apparent body Please don't hate me :( I am new at this computer science....

    Read the article

  • Floating point innacuracies

    - by Greg
    While writing a function which will perform some operation with each number in a range I ran into some problems with floating point inaccuracies. The problem can be seen in the code below: #include <iostream> using namespace std; int main() { double start = .99999, end = 1.00001, inc = .000001; int steps = (end - start) / inc; for(int i = 0; i <= steps; ++i) { cout << (start + (inc * i)) << endl; } } The problem is that the numbers the above program outputs look like this: 0.99999 0.999991 0.999992 0.999993 0.999994 0.999995 0.999996 0.999997 0.999998 0.999999 1 1 1 1 1 1.00001 1.00001 1.00001 1.00001 1.00001 1.00001 They only appear to be correct up to the first 1. What is the proper way to solve this problem?

    Read the article

  • C++ cin returns 0 for integer no matter what the user inputs

    - by kevin dappah
    No matter the cin it continues to to output 0 for score. Why is that? I tried returning the "return 0;" but still no go :/ #include "stdafx.h" #include <iostream> using namespace std; // Variables int enemiesKilled; const int KILLS = 150; int score = enemiesKilled * KILLS; int main() { cout << "How many enemies did you kill?" << endl; cin >> enemiesKilled; cout << "Your score: " << score << endl; return 0; }

    Read the article

  • How to add libraries in C++?

    - by m00st
    Yea this is a dumb question... However in both of my C++ classes we did not do this at all (except for native libraries: iostream, iomanip, etc.)... My question is can anyone provide a link that gives the general explanation of adding libraries to C++? I do realize what what #include means; it's just I have no clue on the linker/directories in a C++ IDE. So long question short; could I get a general explanation of terms used to link libraries in C++? I'm using c::b w/ MinGW.

    Read the article

  • What's the bug in the following code ?

    - by Johannes
    #include <iostream> #include <algorithm> #include <vector> #include <boost/array.hpp> #include <boost/bind.hpp> int main() { boost::array<int, 4> a = {45, 11, 67, 23}; std::vector<int> v(a.begin(), a.end()); std::vector<int> v2; std::transform(v.begin(), v.end(), v2.begin(), boost::bind(std::multiplies<int>(), _1, 2)); std::copy(v2.begin(), v2.end(), std::ostream_iterator<int>(std::cout, " ")); } When run, this gives a creepy segmentation fault. Please tell me where I'm going wrong.

    Read the article

  • How to raise an error, if the parsed number of a C++ stdlib stream is immediatly followed by a non whitespace character?

    - by Micha Wiedenmann
    In the following example, I didn't expect, that 1.2345foo would be parsed. Since I am reading data files, it is probably better to raise an error and notify the user. Is peek() the correct thing to do here? #include <iostream> #include <sstream> int main() { std::stringstream in("1.2345foo"); double x; in >> x; if (in) { std::cout << "good\n"; } else { std::cout << "bad\n"; } } Output good

    Read the article

  • Using pow() for large number

    - by g4ur4v
    I am trying to solve a problem, a part of which requires me to calculate (2^n)%1000000007 , where n<=10^9. But my following code gives me output "0" even for input like n=99. Is there anyway other than having a loop which multilplies the output by 2 every time and finding the modulo every time (this is not I am looking for as this will be very slow for large numbers). #include<stdio.h> #include<math.h> #include<iostream> using namespace std; int main() { unsigned long long gaps,total; while(1) { cin>>gaps; total=(unsigned long long)powf(2,gaps)%1000000007; cout<<total<<endl; } }

    Read the article

  • Why won't this static_cast in C++ work?

    - by samoz
    When I try to use a static_cast to cast a double* to an int*, I get the following error: invalid static_cast from type ‘double*’ to type ‘int*’ Here is the code: #include <iostream> int main() { double* p = new double(2); int* r; r=static_cast<int*>(p); std::cout << *r << std::endl; } I understand that there would be problems converting between a double and an int, but why is there a problem converting between a double* and an int*?

    Read the article

  • SQL Server float datatype

    - by Martin Smith
    The documentation for SQL Server Float says Approximate-number data types for use with floating point numeric data. Floating point data is approximate; therefore, not all values in the data type range can be represented exactly. Which is what I expected it to say. If that is the case though why does the following return 'Yes' in SQL Server DECLARE @D float DECLARE @E float set @D = 0.1 set @E = 0.5 IF ((@D + @D + @D + @D +@D) = @E) BEGIN PRINT 'YES' END ELSE BEGIN PRINT 'NO' END but the equivalent C++ program returns "No"? #include <iostream> using namespace std; int main() { float d = 0.1F; float e = 0.5F; if((d+d+d+d+d) == e) { cout << "Yes"; } else { cout << "No"; } }

    Read the article

  • Correct Exceptions in C++

    - by Dr.Ackula
    I am just learning how to handle errors in my C++ code. I wrote this example that looks for a text file called some file, and if its not found will throw an exception. #include <iostream> #include <fstream> using namespace std; int main() { int array[90]; try { ifstream file; file.open("somefile.txt"); if(!file.good()) throw 56; } catch(int e) { cout<<"Error number "<<e<<endl; } return 0; } Now I have two questions. First I would like to know if I am using Exceptions correctly. Second, (assuming the first is true) what is the benefit to using them vs an If else statement?

    Read the article

  • C++ - defining static const integer members in class definition

    - by HighCommander4
    My understanding is that C++ allows static const members to be defined inside a class so long as it's an integer type. Why, then, does the following code give me a linker error? #include <algorithm> #include <iostream> class test { public: static const int N = 10; }; int main() { std::cout << test::N << "\n"; std::min(9, test::N); } The error I get is: test.cpp:(.text+0x130): undefined reference to `test::N' collect2: ld returned 1 exit status Interestingly, if I comment out the call to std::min, the code compiles and links just fine (even though test::N is also referenced on the previous line). Any idea as to what's going on? My compiler is gcc 4.4 on Linux.

    Read the article

  • Returning the address of local or temporary variable

    - by Dave18
    #include <iostream> int& foo() { int i = 6; std::cout << &i << std::endl; return i; } int main() { int i = foo(); std::cout << &i << std::endl; } I know it doesn't return the address of local variable so that is why the warning but why does it still works and assign the variable i in main() to '6'? How does it only return the value if the variable the was removed from stack memory?

    Read the article

  • which is time consuming construct in following program?

    - by user388338
    while submitting a solution for practise problem 6(odd) i got TLE error but while using using print and scanf in place cin and cout my sol was submitted successfully with 0.77s time..i want to know how can i make it more efficient link to problem is codechef problem 6 #include<iostream> #include<cstdio> using namespace std; int main() {int n,N; scanf("%d",&n); for(int l=0;l<n;l++) { scanf("%d",&N); int i=0,x; if(N<=0) continue; for(;N>=(x=(2<<i));i++); printf("%d",x/2); cout<<"\n"; } }

    Read the article

  • Why I have to redeclare a virtual function while overriding [C++]

    - by Neeraj
    #include <iostream> using namespace std; class Duck { public: virtual void quack() = 0; }; class BigDuck : public Duck { public: // void quack(); (uncommenting will make it compile) }; void BigDuck::quack(){ cout << "BigDuckDuck::Quack\n"; } int main() { BigDuck b; Duck *d = &b; d->quack(); } Consider this code, the code doesn't compiles. However when I declare the virtual function in the subclass, then it compiles fine. The compiler already has the signature of the function which the subclass will override, then why a redeclaration is required? Any insights.

    Read the article

  • What is a cross-platform way to get the current directory?

    - by rubenvb
    I need a cross-platform way to get the current working directory (yes, getcwd does what I want). I thought this might do the trick: #ifdef _WIN32 #include <direct.h> #define getcwd _getcwd // stupid MSFT "deprecation" warning #elif #include <unistd.h> #endif #include <string> #include <iostream> using namespace std; int main() { string s_cwd(getcwd(NULL,0)); cout << "CWD is: " << s_cwd << endl; } I got this reading: _getcwd at MSDN getcwd at Kernel.org getcwd at Apple.com There should be no memory leaks, and is should work on a Mac as well, correct?

    Read the article

  • simple c++ file opening issue

    - by Robert
    #include <iostream> #include <fstream> using namespace std; int main () { ofstream testfile; testfile.open ("test.txt"); testfile << "success!\n"; testfile.close(); return 0; } 1)called "g++ testfile.cpp" 2)created "test.txt" 3)called "chmod u+x a.out" 4)??? 5)file remains blank. I feel like an idiot for failing at something as trivial as this is supposed to be.

    Read the article

  • Copy Constructor in C++

    - by user265260
    i have this code #include <iostream> using namespace std; class Test{ public: int a; Test(int i=0):a(i){} ~Test(){ cout << a << endl; } Test(const Test &){ cout << "copy" << endl; } void operator=(const Test &){ cout << "=" << endl; } Test operator+(Test& p){ Test res(a+p.a); return res; } }; int main (int argc, char const *argv[]){ Test t1(10), t2(20); Test t3=t1+t2; return 0; } Output: 30 20 10 Why isnt the copy constructor called here?

    Read the article

  • Printing escape character

    - by danutenshu
    When I am given "d""\"/""b", I need to print out the statement character for character. (d, b, a slash, a backslash, and 5 quotes) in C++. The only errors that show now are the lines if(i.at(j)="\\") and else if(i.at(j)="\""). Also, how should the outside double apostrophes be excluded? #include <iostream> #include <cstdlib> using namespace std; int main (int argc, const char* argv[] ) { string i= argv[1]; for (int j=0; j>=sizeof(i)-1; j++) { if(i.at(j)="\\") { cout << "\\"; } else if(i.at(j)="\"") { cout << "\""; } else { cout << i.at(j); } } return 0; }

    Read the article

  • Warning on standard headers after upgrading to NetBeans 6.8...

    - by paul
    After upgrading to NetBeans 6.8 on my Mac, some standard headers generate a warning. The warning is "There are unresolved includes inside <string>". <string> is just an example and <iostream> and <map> have the same warning. The project builds and runs fine; however, I would like to resolve these warnings. Has anybody else seen this problem? And is there any way to make that warning go away? I also didn't see this problem when I upgraded to 6.8 on Linux.

    Read the article

  • Referencing invalid memory locations with C++ Iterators

    - by themoondothshine
    I am a big fan of GCC, but recently I noticed a vague anomaly. Using __gnu_cxx::__normal_iterator (ie, the most common iterator type used in libstdc++, the C++ STL) it is possible to refer to an arbitrary memory location and even change its value without causing an exception! Is this expected behavior? If so, isn't a security loophole? Here's an example: #include <iostream> using namespace std; int main() { basic_string<char> str("Hello world!"); basic_string<char>::iterator iter = str.end(); iter += str.capacity() + 99999; *iter = 'x'; cout << "Value: " << *iter << endl; }

    Read the article

  • Why the streams in C++?

    - by oh boy
    As you all know there are libraries using streams such as iostream and fstream. My question is: Why streams? Why didn't they stick with functions similar to print, fgets and so on (for example)? They require their own operators << and >> but all they do could be implemented in simple functions like above, also the function printf("Hello World!"); is a lot more readable and logical to me than cout << "Hello World"; I also think that all of those string abstractions in C++ all compile down to (less efficient) standard function calls in binary.

    Read the article

  • Why do I get the error "X is not a member of Y" even though X is a friend of Y?

    - by user1232138
    I am trying to write a binary tree. Why does the following code report error C2039, "'<<' : is not a member of 'btree<T'" even though the << operator has been declared as a friend function in the btree class? #include<iostream> using namespace std; template<class T> class btree { public: friend ostream& operator<<(ostream &,T); }; template<class T> ostream& btree<T>::operator<<(ostream &o,T s) { o<<s.i<<'\t'<<s.n; return o; }

    Read the article

  • Convert a number from string to integer without using inbuilt function

    - by Raja
    I am trying this technique but error is coming. Please help me to convert a number from string to integer. #include<iostream> using namespace std; int main() { char *buffer[80]; int a; cout<<"enter the number"; cin.get(buffer,79); char *ptr[80] = &buffer; while(*ptr!='\0') { a=(a*10)+(*ptr-48); } cout<<"the value"<<a; delete ptr[]; return 0; } Errors are: error C2440: 'initializing' : cannot convert from 'char ()[80]' to 'char *[80]' error C2440: '=' : cannot convert from 'char *' to 'int'

    Read the article

  • How to make a rectangle on screen invisible to screen capture ?

    - by Kesarion
    How can I create a rectangle on the screen that is invisible to any sort of screen capture(printscreen or aplications) ? By create a rectangle on screen I mean something like this: #include <Windows.h> #include <iostream> void drawRect(){ HDC screenDC = ::GetDC(0); ::Rectangle(screenDC, 200, 200, 300, 300); ::ReleaseDC(0, screenDC); } int main(void){ char c; std::cin >> c; if (c == 'd') drawRect(); std::cin >> c; return 0; } I'm using Visual Studio 2010 on Windows XP

    Read the article

< Previous Page | 8 9 10 11 12 13 14 15 16 17 18 19  | Next Page >