Thread: No error, but still doesn't work!

  1. #1
    Registered User
    Join Date
    Jul 2006
    Posts
    7

    No error, but still doesn't work!

    Hey everybody.

    I am new to this forum, and i just started programming. I have been trying to make some different simple programs, but when i tried this, it just wouldn't put out a number at the end of the programming. I would appreciate some help. ( Also if you have some tricks to make some of the things in the program more simple plz tell me )

    Here is the code.

    Code:
    #include <iostream>
    
    using namespace std;
    
     double y;               // Antal genstande!
     double x;               // Vægt!
     double d;
     double j;
    
    int main()
    {
     cout<<"                            PROMILLE UDREGNER  \n                               - af Alek -";
    cin.get();
    cout<<"Indsaet din vaegt i kg  :";
    d = 0.68 * x;
    cin>> x;
    cin.ignore();
    cout<<"Indsaet antal indtagede genstande  :";
    j = 12 * y;
    cin>> y;
    cin.ignore();
    cout<<"Din promille er:   ";
    cout<< j / d;
    cin.get();
    }
    Thx!

    BTW, sorry if it is a bit messy .

    The problem occurs when i want the last expression ( cout<< j / d ) to be shown off in the program, but it doesn't put out numbers, instead it writes something strange. If anybody knows what to do / or if i made an error ( which i probably did, please tell me )

    P.S. The language is danish, might look a bit strange to some ppl.

  2. #2
    Registered User
    Join Date
    May 2006
    Posts
    903
    Code:
    d = 0.68 * x;
    cin>> x;
    
    //...
    
    j = 12 * y;
    cin>> y;
    Both are backwards. You are calculating values before prompting the user. Swap those lines and you should be ok.

  3. #3
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    What kinds of things does it write?

    I don't think x (and j) is defined here:

    d = 0.68 * x;

    EDIT - Damn. Beaten.

    But seeing as I'm exceedingly bored:

    Code:
    #include <iostream>
    using namespace std;
    
    int main( void )
    {
     	double y;               // Antal genstande! - You should probably not use Globals
     	double x;               // Vægt!
     	double d;
     	double j;
    
    	cout<<    "                            PROMILLE UDREGNER  ";
    	cout<< "\n                               - af Alek -      ";
    
    	cin.get();
    
    
    	cout<<"Indsaet din vaegt i kg  :";
    	cin >> x;
    
    	d = 0.68 * x;
    
    	cin.ignore();
    
    	cout<<"Indsaet antal indtagede genstande  :";
    	cin >> y;
    
    	j = 12 * y;
    
    	cin.ignore();
    
    
    
    	cout<<"Din promille er:   ";
    	cout<< j / d;
    
    	cin.get();
    
    	return 42;	// Or 0, or whatever you want. 
    }
    Last edited by twomers; 07-28-2006 at 09:51 AM.

  4. #4
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    You don't initialize the variables to 0.. so it just grabs what ever was in the memory where the variable is placed.

    [edit] double pwnt =( [/edit]

  5. #5
    Registered User
    Join Date
    Jul 2006
    Posts
    7
    Hey.. You guys were right . Just switched cin>> x; with the function, cuz i had to declare the function before, else the value of the function would be ignored. Thx

  6. #6
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    A good practice though would be to initialize all your variables to a value, so you can find mistakes like this easier. Make all your int/float/long/double/ect init to 0 and string/char[] to ""

    Code:
    int anInt = 0;
    long aLong = 0;
    std::string aString = "";
    char aChar[10] = "";
    That way you can't accidentally use garbage values like you were.

  7. #7
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    I couldn't agree more! (I would put more of these '!', there, but you told someone today that they don't add any more meaning)

  8. #8
    Registered User
    Join Date
    Jul 2006
    Posts
    7
    I'll do that next time!
    Now i want to make some comments, that depends on the value i get from the last function ( j / d ). But i doesn't seem like i can do that, the comment is always the same, no matter what value i get. I am allowed to make the value be between to other values like i have done right? And one last thing ? what do i need the return 0; (or whatever value i like) function for?

    Code:
    #include <iostream>
    using namespace std;
    
     	double y;               // Antal genstande! - You should probably not use Globals
     	double x;               // V&#230;gt!
     	double d;
     	double j;
    
    int main()
    {
    
    
    	cout<<    "                            PROMILLE UDREGNER  ";
    	cout<< "\n                               - af Alek -      ";
    
    	cin.get();
    
    
    	cout<<"Indsaet din vaegt i kg  : ";
    	cin>> x;
    
    	d = 0.68 * x;
    
    	cin.ignore();
    
    	cout<<"Indsaet antal indtagede genstande  : ";
    	cin>> y;
    
    	j = 12 * y;
    
    	cin.ignore();
    
    
    
    	cout<<"Din promille er:   ";
    	cout<< j / d;
        
        if ( 0.2 <= ( j / d ) <= 0.5 ) {
        cout<<"\n Oejets evne til hurtigt at fokusere og omstille sig fra lys til moerke forringes.";
    }
        else if ( 0.5 <= ( j / d ) <= 0.8 ) { 
        cout<<"\n Evnen til p&#229; en gang at opfatte situationer og samtidig udfoere praecise bevaegelser begynder at forringes, ligesom synsvinklen indsnaevres.";
    }
        else if ( 0.8 <= ( j / d ) <= 1.0 ) {
        cout<<"\n Nedsat koordinationsevne og oeget reaktionstid.";
    }
        else if ( 1.0 <= ( j / d ) <= 1.5 ) {
        cout<<"\n Opmaerksomheden og koncentrationsevnen bliver svaekket, traethedssymptomer begynder, ligesom balance- og bevaegelsesevnen nedsaette.";
    }   
        else if ( 1.5 <= ( j / d ) <= 2.0 ) {
        cout<<"\n Svaert forringet bevaegelsesevne og talebesv&#230;r. Centralnervesystemet og maven har faaet nok.";
    }   
        else if ( 2.0 <= ( j / d ) <= 3.0 ) {
        cout<<"\n Store forgiftningssymptomer. Selvkontrollen er helt vaek.";
    }   
        else if ( 3.0 <= ( j / d ) <= 4.0 ) {
        cout<<"\n Manglende kontrol over f.eks. urinblaeren, evt. bevidstloeshed.";
    }   
        else if ( 4.0 <= ( j / d ) <= 100000 ) {
        cout<<"\n Bevidstloeshed, livsfare";
    }
    
        else {
        cout<<"Du er ikke paavirket af alkohol";
    	cin.get();
    }
    cin.get();
    }
    Thx again !

  9. #9
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    Tags Taga Tags!`

    Take a look at my first post. You shouldn't need to use Globals in your example!

    EDIT - why did you put them back as variables?
    EDIT2- Perhaps it would be more readable if you made another variable which holds the value of j/d.

    try it with some

    Code:
    if ( j/d>=0.2 $$ j/d<= 0.5 )
    instead. What does this program do?
    Last edited by twomers; 07-28-2006 at 10:38 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strcmp returning 1...
    By Axel in forum C Programming
    Replies: 12
    Last Post: 09-08-2006, 07:48 PM
  2. getline() don't want to work anymore...
    By mikahell in forum C++ Programming
    Replies: 7
    Last Post: 07-31-2006, 10:50 AM
  3. Why don't the tutorials on this site work on my computer?
    By jsrig88 in forum C++ Programming
    Replies: 3
    Last Post: 05-15-2006, 10:39 PM
  4. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM
  5. DLL __cdecl doesnt seem to work?
    By Xei in forum C++ Programming
    Replies: 6
    Last Post: 08-21-2002, 04:36 PM