Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C++ and Visual C++ > C++ Programming
|
C++ Programming General discussions for the C++ language. For questions specific to Microsoft's Visual C++ variant, see the Visual C++ forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C++ Programming section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old September 26th, 2007, 05:55 AM
Registered User
 
Join Date: Sep 2007
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to pdang265
Default Please help with passing (char * name[])

Please help, I'm new to C++ and struggling with this problem.
I have an array of char and an array of double declared in main as follow: (Please ignore uc1601 and UF_UI codes because they are Ug/Open API user function.)

int flag;
char *variables[36];
double values[36];

then a call to read from file in main:

flag = read_variables(variables,value);









In the read_variables function I have:

#include <cstdlib>
#include <fstream>
#include <iostream>
    using std::ios;
    using std::ifstream;
    using std::istream;
    using std::fstream;

#include <string>
#include <sstream>

int read_variables(char*[],double []);


int read_variables (char *variables_name,double variables_value[])
{
    const char textfile[133] = {"C:\\My
    Documents\\Project_VC\\Key_retainer\\variables_fil e.txt"};

    char name[133], msg[133];
    double value;
    int ok;

    ifstream OpenToRead(textfile, ios::in);
      if (!OpenToRead)
    {
          sprintf(msg ,"Can not open this %s \n file to
                        read ",textfile );
          uc1601(msg, 1);
          return (1);
    }
    else
    {
           UF_UI_open_listing_window();

       for (int i = 0; i < variable_count; i++)
       {
         OpenToRead >> name >> value;
         variables_name[i] = name;
         variables_value[i] = value;
       }

        OpenToRead.close();

    for (int i = 0; i<variable_count; i++)
    {
      sprintf(msg ,"variable %s = %2.4f\n", variables_name[i],
                        variables_value[i]);
      UF_UI_write_listing_window(msg);

    }
 }

Here are my problems:
1) The variables_name[] print out only the last variable that read from the file. but the variables_value[] print out correct.
2) I print it agian back in main and I got the same result.
3) Why it only retain 1 name (last one in the file)

Here are ehat in the file:
.
.
.
UNDERCUT_THICKNESS 33
UNDERCUT_ANGLE 34
WINDOW_WIDTH 35
UNDERCUT_WIDTH 36
BLIND_SLOT_HOLE_DIA 0.195

And here are what it print out:
variable BLIND_SLOT_HOLE_DIA = 33.0000
variable BLIND_SLOT_HOLE_DIA = 34.0000
variable BLIND_SLOT_HOLE_DIA = 35.0000
variable BLIND_SLOT_HOLE_DIA = 36.0000
variable BLIND_SLOT_HOLE_DIA = 0.1950



Please help! I'm struggling with this problem all day yesterday.
Many thanks in advance.
Dang

Reply With Quote
  #2 (permalink)  
Old September 26th, 2007, 05:59 AM
Registered User
 
Join Date: Sep 2007
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to pdang265
Default

Oooopppsss! Please replace the function parameters with:

int read_variables (char *variables_name[],double variables_value[])

Reply With Quote





Similar Threads
Thread Thread Starter Forum Replies Last Post
Big challenge here! How to convert char* to char^? samiswt Visual C++ 2005 1 November 30th, 2007 09:09 PM
Invalid conversion from 'char*' to 'char' deuxmio C++ Programming 3 December 8th, 2006 07:56 AM
Escape char for '-' mattastic Access 1 October 6th, 2005 03:27 PM
getting the last CHAR of a string kondapally Crystal Reports 0 December 13th, 2004 03:39 PM
char problem roelbagao Java Databases 1 July 19th, 2003 12:29 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.