?
Solved

Replace input style (cin to array)

Posted on 2006-11-25
5
Medium Priority
?
968 Views
Last Modified: 2008-04-27
Hi everybody,
I got a code (calculate math expression) from a website and I want to use it in my program.

Part of the code:
"

    cout << "Don't forget add ';' to the tail of the expression:)" <<endl;  

 do
      {
           cin >> temp;
                      if(isdigit(temp))
                    {
                 cin.putback(temp);
                           cin >> number[ntop++];
                      }
...............
      }
      while(temp!=END);
                   
    cout << "And the answer is... " << number[0] <<endl;                    
    return 0;
}
"


In my program I already have an expression (char array) so I don't want to use the "cin" commands (especialy cin.putback) to deal with the expression..
I want to use my array or maybe put the string into cin streaming function(if possible) so I'll can use this code..
Any ideas..?
Thanks for your help.  
0
Comment
Question by:flexlight
[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 Comments
 
LVL 7

Accepted Solution

by:
UrosVidojevic earned 200 total points
ID: 18013113
Hi flexlight,

this is sample code that will solve your problem.

The idea is similar to that, you used in your code.
Program is goint through array of chars and checking
for arrays of successive digits (numbers) and puting
them in an array of numbers. At the end program prints
the first of them.

Althought, I don't understand why first of them
should be answer. :-)

Sample code:
=============
_______________________________________________________________________________
#include <iostream>
#include <cstring>
using namespace std;

const int MAX_LEN = 1000, // Maximal char array length
        MAX_NUM = 500;  // Maxinal number of numbers in array      

int main() {
      char text[MAX_LEN];
      cout << "Char array: ";
      cin.getline(text, MAX_LEN);

      // text is array of characters that you allready have.
      // so this is part of code that is particulary
      // interesting for your problem.

      int i = 0, len = strlen(text), number[MAX_NUM], ntop = 0;
      while (true) {
            while (i < len && !isdigit(text[i]))
                  i++;
            if (i == len) break;
            long num = 0;
            while (i < len && isdigit(text[i])) {
                  num = 10*num + (text[i] - '0');
                  i++;      
            }
            number[ntop++] = num;
      }

      // i is index of current character in array
      // len is length of array
      // number is array if numbers found in array
      // num is current number
      // ntop is index of current number

      cout << "And the answer is: " << number[0] << endl;

      // note that if there is no numbers in array
      // number[0] will be some random number.


      // this is is optional, just to show you
      // that program is correct.
      cout << "Other numbers: ";
      for (int i = 1; i < ntop; i++)
            cout << number [i] << " ";
      cout << endl;
}
_______________________________________________________________________________


Sample input\output 1:
=======================
_______________________________________
Char array: f(123) + g(12) - sin(45)/tg(32) + 123*456
And the answer is: 123
Other numbers: 12 45 32 123 456
_______________________________________


Sample input\output 2:
=======================
_______________________________________
Char array: a(1+b(2+c(3)+2)+1)
And the answer is: 1
Other numbers: 2 3 2 1
_______________________________________

If I was not clear enough,
I would be glad to help.
Regards, Uros.
0
 
LVL 5

Expert Comment

by:allmer
ID: 18721853
Looks like you would like to convert your char array to the array of numbers as in the small example.
Would this be helpful:
for(int i=0;...) {
  number[i] = atoi(yourCharArr[i]; //for int atof for double
}
Now you have numbers instead of characters if that was what you tried to achieve
0
 
LVL 6

Expert Comment

by:SeanDurkin
ID: 21427223
No comment has been added to this question in more than 21 days, so it is now classified as abandoned.

I will leave the following recommendation for this question in the Cleanup Zone:
  Accept: UrosVidojevic {http:#18013113}

Any objections should be posted here in the next 4 days. After that time, the question will be closed.

Sean Durkin
Experts Exchange Cleanup Volunteer
0
 
LVL 1

Expert Comment

by:Computer101
ID: 21449012
Forced accept.

Computer101
EE Admin
0

Featured Post

Enroll in October's Free Course of the Month

Do you work with and analyze data? Enroll in October's Course of the Month for 7+ hours of SQL training, allowing you to quickly and efficiently store or retrieve data. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

650 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