very dumb question.

This is a discussion on very dumb question. within the C++ Programming forums, part of the General Programming Boards category; I have a question that I know is going to make me look stupid. Im trying to round a number ...

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    16

    Angry very dumb question.

    I have a question that I know is going to make me look stupid. Im trying to round a number up, and I've included this #include <math.h>. I've been searching for a long time and I can't see why my compiler keeps telling me Math.Ceiling is an underclared identifier. I've read that I should add "-lm" to something but I have been able to see what I should add it to. Im using the Microsoft Visual c++ 2005 express edition beta 2 compiler. Any ideas?

  2. #2
    Bioport Productions
    Join Date
    Oct 2005
    Posts
    215
    its math.ceil
    -"What we wish, we readily believe, and what we ourselves think, we imagine others think also."
    PHP Code:
    sadf 

  3. #3
    ^ Read Backwards^
    Join Date
    Sep 2005
    Location
    Earth
    Posts
    282
    One,

    Code:
    #include <cmath>
    And as far as syntax for ceil goes.
    Code:
    int rounded_num;
    
    double num_to_round = 1.88;
    
    rounded_num = ceil(num_to_round);
    
    cout << rounded_num;
    
    /OR, just
    
    cout << ceil(num_to_round);

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    16
    i knew this question would make me look dumb

    thnx

  5. #5
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Well for starters you can use <cmath> instead of <math.h> since you are dealing with c++. Second you can show your actual code. Third you can just use the ceil() function

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    16
    Well I changed the code around, and now I no longer get that error. Instead I get an error saying its going to overload the function. Heres my code. Error is coming from the string file_content(string a){ function. This is my first time using c++ in a long time

    Code:
    // test.cpp : Defines the entry point for the console application.
    // inluding all the required headers
    
    #include "stdafx.h"
    #include <string>
    #include <fstream>
    #include <iostream>
    #include <cmath>
    
    using namespace std;
    
    //function prototypes and invalid
    string file_content(string a);
    string encrypt(string a);
    string decrypt(string a);
    void write_file(string a,string b);
    bool invalid = false;
    float password_index;
    string password;
    
    int main(){
    	string content;
    	string file;
    	string input;
    	if (invalid == true){
    		invalid = false;
    		goto again;
    	}
    	cout<<"Welcome to ascii encrypter and decrypter v1.0"<<"\n";
    again:
    	cout<<"please enter the file you wish to encrypt or decrypt : ";
    	cin>>file;
    	cin.get();
    	cout<<"getting file content \n";
    	content = file_content(file);
    choice:
    	cout<<"all file information has been found"<<"\n"<<"would you like to encrypt or decrypt this file? ";
    	cin>>input;
    	cin.get();
    	if (input=="encrypt"){
    		content = encrypt(content);
    	}
    	else if (input=="decrypt"){
    		content = decrypt(content);
    	}
    	else {
    		cout<<"your intention is unclear\n";
    		goto choice;
    	}
    	cout<<"now writing to file\n";
    	write_file(file, content);
    	cout<<"writting is complete\n";
    	cin.get();
    	return 1;
    }
    
    string file_content(string a){
    	string file;
    	string backup;
    	string buffer;
    	ifstream b_file (a.c_str());
    	if ( b_file.is_open() ){
    		while ( !b_file.eof() ){
    			b_file>>buffer;
    			file += buffer;
    			file += " ";
    		}
    		b_file.close();
    		password_index = ceil(file.length()/2);
    		return file;
    	}
    	else{
    		b_file.close();
    		cout<<"wrong file name or path\n";
    		invalid = true;
    		return "null";
    		main();
    	}
    }
    
    string encrypt(string a){
    	for (int z =0;z<a.size();z++){
    		if (a[z]%2 == 0){
    			a[z] += 4*z;
    		}
    		if (a[z]%2 != 0){
    			a[z] -= 4*z;
    		}
    	}
    	return a;
    }
    
    string decrypt(string a){
    	for (int z =0;z<a.size();z++){
    		if (a[z]%2 == 0){
    			a[z] -= 4*z;
    		}
    		if (a[z]%2 != 0){
    			a[z] += 4*z;
    		}
    	}
    	return a;
    }
    
    void write_file(string a,string b){
    	ofstream b_file (a.c_str(), ios::trunc);
    	for (int z =0;z<(b.size()-1);z++){
    		b_file<<b[z];
    	}
    }

  7. #7
    ^ Read Backwards^
    Join Date
    Sep 2005
    Location
    Earth
    Posts
    282
    Compiled and ran fine for me(encrypted and decrypted properly). I am using VC++ 6.

    Though:
    #include "stdafx.h"
    Is not needed/used.

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    16
    the #include "stdafx.h" is needed for the visual studio 2005 for some reason. nothing works with out it :S. What compiler are u using?

    here is the exact error:


    Code:
    1>e:\programming\test\test\test.cpp(69) : error C2668: 'ceil' : ambiguous call to overloaded function
    1>        e:\apps\microsoft visual studio 8\vc\include\math.h(535): could be 'long double ceil(long double)'
    1>        e:\apps\microsoft visual studio 8\vc\include\math.h(487): or 'float ceil(float)'
    1>        e:\apps\microsoft visual studio 8\vc\include\math.h(125): or 'double ceil(double)'
    1>        while trying to match the argument list '(__w64 unsigned int)'
    Last edited by Blips; 11-07-2005 at 10:06 PM.

  9. #9
    ^ Read Backwards^
    Join Date
    Sep 2005
    Location
    Earth
    Posts
    282
    What compiler are u using?
    I am using VC++ 6.
    It also compiles and runs fine in Dev-C++ (5.0 beta 9.2 (4.9.9.2)). Free:http://www.bloodshed.net/dev/devcpp.html
    the #include "stdafx.h" is needed for the visual studio 2005 for some reason.
    Ohh.

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    16
    strange that it compiles fine in one, and not another?

  11. #11
    ^ Read Backwards^
    Join Date
    Sep 2005
    Location
    Earth
    Posts
    282
    Yes, I would say so.

    But since it compiles fine on two other app's, I would guess it is yours (It is in Beta after all). Maybe somebody else would know. But, you are done, compile it with Dev-C++

  12. #12
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    The ambiguity is actually really easy to understand and fix. ceil() expects some type of floating point number. You are giving it an int. So it has to change the int into a floating point number. But which type ( float, double, long double)? An easy fix to that is just to do:
    Code:
    password_index = ceil(file.length()/2.0);

  13. #13
    Registered User
    Join Date
    Jan 2005
    Posts
    7,340
    >> the #include "stdafx.h" is needed for the visual studio 2005 for some reason

    I don't think it is needed if you pick the right project type. On your existing project, you can go to the project properties and turn off precompiled headers. On new projects, select Win32 Console Application, then in the wizard make sure to go to the application settings and select Blank Project. You will have to add your source file, but it will be blank and will not require the stdafx.h.

  14. #14
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,803
    Code:
    else{
        b_file.close();
        cout<<"wrong file name or path\n";
        invalid = true;
        return "null";
        main();
    }
    You should not be recursively calling the main function; however, since you return on the statement above that recursive function call to main, it will never happen and so it should be removed in any case.

    Try to avoid the use of goto's where possible, you can achieve the same result through looping mechanisms... you know, for/while/do-while.

    Descriptive variable names would be helpful, none of this a and b nonsense.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  15. #15
    Registered User
    Join Date
    Jan 2005
    Posts
    16
    Thnx for all the suggestions and help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. dumb question
    By travis999 in forum C Programming
    Replies: 3
    Last Post: 10-26-2007, 12:57 AM
  2. Dumb question
    By dragon2309 in forum C Programming
    Replies: 18
    Last Post: 10-29-2005, 03:27 PM
  3. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  4. Sorry for a dumb question.
    By Vanished in forum C++ Programming
    Replies: 7
    Last Post: 11-23-2002, 11:39 AM
  5. another dumb question - loop not working?
    By Captain Penguin in forum C++ Programming
    Replies: 8
    Last Post: 10-06-2002, 10:15 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21