Solved

Using variable to store number and text

Posted on 2004-09-20
15
166 Views
Last Modified: 2010-04-01
Is it possible to store float or text in a variable (depending on certain criteria). I am trying to do this...if criteria A is selected the display a float value else display a text "The End"...something like this...
0
Comment
Question by:KalluMama
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 5
  • 3
  • +2
15 Comments
 

Author Comment

by:KalluMama
ID: 12107854
i get possible loss of data in conversion from float to char...
0
 
LVL 10

Expert Comment

by:Sys_Prog
ID: 12107870
POst some of your code

amit
0
 

Author Comment

by:KalluMama
ID: 12107899
if (wgt <= 50 && largestDim <=36 && maxSize < 72)
{
//The criteria was met, so find out the cost
wgtIndex = wt_index(aInt, ARR_MAX, wgt);
cost = aFloat[wgtIndex];
}
else
{
//The criteria was not met so display, rejected
cost = "Rejected";
}

cout << cost;
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 10

Expert Comment

by:Sys_Prog
ID: 12107963
What is type of cost...

U cant have cost have string as well as float

however, u could use uniion for that

Amit
0
 

Author Comment

by:KalluMama
ID: 12107996
i need to show output in one column. If certain criteria matches, then display the cost, else display rejected. Any ideas how to do this...there will be no calculations performed on these numbers.
0
 
LVL 10

Expert Comment

by:Sys_Prog
ID: 12108271
displayng is not a problem
the problem is that u are using the same variable to store the output...thats not possible

Amit
0
 
LVL 10

Expert Comment

by:Sys_Prog
ID: 12108288
Or what u can do is always store in string type of variale since u do not need calclations

Amit
0
 
LVL 10

Assisted Solution

by:Sys_Prog
Sys_Prog earned 100 total points
ID: 12108328
Try tis

#include <iostream>
#include <sstream>


using namespace std ;


main() {

      stringstream      str ;

      int f = 10 ;

      for ( int i = 0 ; i <= 1 ; i++ ) {
            if ( i == 0 ) {
                  str << "hello" ;
            }
            else {
                  str <<       f ;
            }
            cout << endl << str.str() ;
      }


}

Amit
0
 

Author Comment

by:KalluMama
ID: 12108837
is there a way to do this without using stringstream? i have not been taught that so i cant use it... :(


0
 
LVL 1

Expert Comment

by:decentswati
ID: 12109378
hi kallumama,
if u know void pointer then try to achieve that with its help.
u can decide at runtime what type of value u want to display,without loosing any data.

try it may sure help u..
-decentswati
0
 
LVL 9

Accepted Solution

by:
jhshukla earned 200 total points
ID: 12109573
typedef struct Cost{
  bool accepted;
  float value;
} Cost;

Cost cost;

if(criteria met){
  cost.accepted=true;
  cost.value = somefloat;
}
else{
  cost.accepted = false;
  cost.value = -1; //or whatever errorcode you want
}

cout << (cost.accepted ? cost.value : "rejected");

but i would prefer union as amit suggested.

and if you are sure that acceptable cost is >=0 then instead of cost = "rejected" you could say cost = -1 and then
cout << (cost < 0 ? "Rejected" : cost);
0
 
LVL 39

Assisted Solution

by:itsmeandnobodyelse
itsmeandnobodyelse earned 200 total points
ID: 12111273
Or simply omit cost variable and have different output:

if (wgt <= 50 && largestDim <=36 && maxSize < 72)
{
//The criteria was met, so find out the cost
wgtIndex = wt_index(aInt, ARR_MAX, wgt);
   cout <<  aFloat[wgtIndex] << endl;
}
else
{
//The criteria was not met so display, rejected
   cout <<  "Rejected" endl;
}

If you have to use cost variable, choose a char array (or better a string type, but you may not be allowed to use string?).

char cost[256] = "";  // make it big enough for any reasonable input
if (wgt <= 50 && largestDim <=36 && maxSize < 72)
{
   //The criteria was met, so find out the cost
   wgtIndex = wt_index(aInt, ARR_MAX, wgt);
   gcvt(aFloat[wgtIndex], 6, cost);  // 6 is the # of significant digits
}
else
{
   //The criteria was not met so display, rejected
   strcpy (cost, "Rejected");
}

cout << cost << endl;

Regards, Alex

p.s. tell me, if you need a solution using string type


0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 12111282
Correction:

    cout <<  "Rejected" endl;  

turns to

   cout <<  "Rejected" << endl;

Regards, Alex


0
 

Author Comment

by:KalluMama
ID: 12112053
Alex,

Since we have not been taught 'gcvt', it will be odd for me to use it. Since i am doing no calculations...could i only use string. There are many outputs to follow and this will be one of them which will be displayed in the program later depending on criteria earlier...
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 12123473
Sorry, i replied to your last comment but forgot to submit somehow. Now it is lost... but i'll try again:

My first sample code doesn't use gcvt. Isn't it an alternative?

>>>> Is it possible to store float or text in a variable

Your original question can be answered with yes. That variable could be a char array or a string type. But, you have to convert float to string then. For that i know 4 ways:

1. gcvt (or ecvt, fcvt)
2. stringstream
3. sprintf
4. an own conversion function

You are not allowed to use (1) or (2), right? With sprintf the conversion is like that:

      char cost[256] = "";
      sprintf(cost, "%f", aFloat[wgtIndex]);

If (4) is an alternative, tell me and i will help you with that.

If you could use none of these, your initial question must be answered with: "Yes, but not with the means you have been taught til now".

Regards, Alex
 


 
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question