Getting a random output

This is a discussion on Getting a random output within the C++ Programming forums, part of the General Programming Boards category; this is my code Code: #include <iostream> using namespace std; #include "Car.h" Car::Car(){ int od = 0; double val = ...

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    118

    Getting a random output

    this is my code
    Code:
     #include <iostream>
     using namespace std;
     #include "Car.h" 
     
     Car::Car(){
         int od = 0;
         double val = 0.0;
         char make = '?';
     }
     
     Car::Car(int o, double v, const char* s){
         od = o;
         val = v;
         strcpy(make,s);
     }
     
     Car::~Car(){
         
     }
     void Car::set(int o, double v, const char* s){
         if(o >=0 && o < 999999){
              if(v >= 499.99){ 
                   od = o; 
                   val = v;
                   strcpy(make,s);
              }    
         } 
         else {
              int od = 0;
              double val = 0.0;
              char make = '?';    
         }       
     }
     
     int Car::valid() const{
         int ret = 0;
         
         if(od >=0 && od < 999999){
              if(val >= 499.99){ 
                   ret = 1;
              }    
         }
         return ret;     
     }
     
     int Car::valid(const char* s) const{
         int ret = 0;
         char first[4];
         strncpy(first, make, 4);
         if(strcmp(first,s) == 0){              
              ret = 1;
         }
         if(strcmp(s,make) == 0){
              ret = 1;    
         }
         return ret;  
     }
     
     void Car::display() const{ 
         cout << od << ' ' <<  val << ' ' << make << endl;   
     }
     
     int main ( ) {
         Car car[5];
    
         car[0].set(   12345, 20000., "2005 Camry"); 
         car[1].set(     -45, 20000., "2005 Ford"); 
         car[2].set(   12345,   200., "2005 BMW"); 
         car[3] = Car(456123,  2000., "1995 Mercedes"); 
         car[4] = Car(  1234, 40000., "2005 BMW"); 
    
         for (int i = 0; i < 5; i++)
             if (car[i].valid() != 0)
                 car[i].display();
    
         for (int i = 0; i < 5; i++)
             if (car[i].valid("2005") != 0)
                 car[i].display();
    
         return 0;
     }
    this is my header file:
    Code:
    class Car{
              int od;
              double val;
              char make[30];
          public:
              Car();
              Car(int o, double v, const char* s);
              ~Car();
              void set(int o, double v, const char* s);
              int valid() const;
              int valid(const char* s) const;
              void display() const;
    };
    i just want to know why i get this as a output:
    Code:
    12345 20000 2005 Camry
    72 1.06072e+292   " << anyone know where this line is here
    456123 2000 1995 Mercedes
    1234 40000 2005 BMW
    12345 20000 2005 Camry
    1234 40000 2005 BMW
    Other than the second line output it works good

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    In your default initializer and the else part of your valid, you shadow your member variables, you don't set them (by declaring new variables of the same name).

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    118
    wow, that was simple

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 2d game
    By JordanCason in forum Game Programming
    Replies: 5
    Last Post: 12-08-2007, 09:08 PM
  2. Having trouble making a function do what I want.
    By Shamino in forum C++ Programming
    Replies: 9
    Last Post: 12-07-2007, 10:20 AM
  3. Base converter libary
    By cdonlan in forum C++ Programming
    Replies: 22
    Last Post: 05-15-2005, 01:11 AM
  4. Generate random numbers in Lucky7 project using C#
    By Grayson_Peddie in forum C# Programming
    Replies: 1
    Last Post: 04-11-2003, 11:03 PM
  5. Output 3 random sets of 5
    By mrcrossroads in forum C++ Programming
    Replies: 7
    Last Post: 10-29-2002, 06:22 PM

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