Solved

Using variable to store number and text

Posted on 2004-09-20
15
165 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
  • 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
Independent Software Vendors: 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

680 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