-3

I'm having errors in my code below, This is my code:

#include "stdafx.h"
#include <vector>
#include <iostream>
using namespace std;

int main(){
    vector<int> numbers(0);
    cout << "please enter you numbers :::\n''entering any characters but numbers is the end of entry''";
    char ch;
    int i = 0;
    while (Isnumber(ch)){    //here is the error
        do{
            ch = getchar();
            int newnumber = 0;
            cout << "element(" << i << ") = ";
            cin >> newnumber;
            numbers.push_back(newnumber);
        } while (ch>0 || ch < 9);
    }   
    getchar();
}

two errors, it says that identifier is unknown, and it says variable char is uninitialazed local variable,

Mohamadreza
  • 313
  • 3
  • 6
  • 22
  • 4
    The errors are right. But you need to ask a question. – juanchopanza Jan 30 '15 at 08:16
  • What's `Isnumber`? The function is not declared anywhere in the code. How do you expect it to work? The title of your question mentions `isnumber`, but the code contains `Isnumber`. Why the inconsistency? – AnT stands with Russia Jan 30 '15 at 08:41
  • What is your question? Is it 'why do I get the error message from a compiler' or 'how should I modify my program to compile it sucessfully and make it do what I want'...? – CiaPan Jan 30 '15 at 08:48
  • The question was how to get solve those errors and make the program run, which I solved it just now using cin functions. I'll post the solution down. – Mohamadreza Jan 30 '15 at 09:23
  • I suppose this way you will get your program run, but not do what you want. The errors are resulting from your misunderstanding of _what_ the program should do (esp. when you write rpogram for someone else) or _how_ it should be done. Getting rid of the compilation error does _not_ mean your code is any closer to the proper solution. Just as @AndreyT pointed out. – CiaPan Jan 31 '15 at 16:31

3 Answers3

1

change this while (Isnumber(ch)){ into do-while loop.

do{
  .....
}while (Isnumber(ch))

The error is because ch is declared and it is used before initialized.

Also include #include <stdio.h>; for getchar();

Sridhar DD
  • 1,972
  • 1
  • 10
  • 17
  • This has different semantics as `ch` isn't checked before adding it to the `numbers` vector. – TartanLlama Jan 30 '15 at 08:23
  • @TartanLlama:: Nope i disagree with that. It is used to avoid the check on first iteration. If it is correct get back your down vote. – Sridhar DD Jan 30 '15 at 08:25
  • 2
    What's `Isnumber`? The function is not declared anywhere in the code. – AnT stands with Russia Jan 30 '15 at 08:33
  • @sridhar, sorry, you're right; I got confused with the bizarre uses of `ch`, `newnumber` and `i`. – TartanLlama Jan 30 '15 at 08:33
  • @AndreyT:: It is function to check whether the char entered is an Numeric value or not. – Sridhar DD Jan 30 '15 at 08:35
  • 1
    @Sridhar DD: Well, the function is not declared. You can't just say to the compiler "It is function to check whether the char entered..." and expect it to understand you. You have to write that function: declare and define it. – AnT stands with Russia Jan 30 '15 at 08:36
  • @AnT, just to play fair here, that is your preference of how to provide an answer. There is a valid argument that the `Isnumber` function's intent is clear if not perfectly obvious. Since when is there a requirement of SO to give complete and compilable code? Pseudo code is most definitely useful, acceptable, and I would hope encouraged. – ChiefTwoPencils May 01 '15 at 09:30
0

Better do it in one loop:

do {
    ch = getchar();
    int newnumber = 0;
    cout << "element(" << i << ") = ";
    cin >> newnumber;
    numbers.push_back(newnumber);
} while (Isnumber(ch)); // should probably be isdigit(ch)

And before asking similar questions read this first (or buy a book).

Community
  • 1
  • 1
Axalo
  • 2,953
  • 4
  • 25
  • 39
0

Well I solved it using cin functions as below,

#include "stdafx.h"
#include <vector>
#include <iostream>
using namespace std;

int main(){
    vector<int> numbers(0);
    cout << "please enter you numbers :::\n''entering any characters but numbers is the end of entry''\n";
    //char ch;
    int counter = 0;    
        do{         
            int newnumber = 0;
            cout << "element(" << counter << ") = ";
            counter++;
            cin >> newnumber;
            numbers.push_back(newnumber);
            if (cin.fail()){
                cout << "entered numbers are:\n";
                for (vector<int>::iterator i = numbers.begin(); i != numbers.end(); i++)
                {
                    cout << *i;
                    if (i != numbers.end()-1)cout << " - ";
                }               
            }
        } while (cin.good());
        getchar();
}

I removed one while loop. and used cin.fail and cin.good to avoid using IsNumber. And it worked.

Mohamadreza
  • 313
  • 3
  • 6
  • 22
  • No, it doesn't work. The fact that you do `push_back` before even checking anything (like `fail`), makes it immediately obvious that it can't possibly work properly. – AnT stands with Russia Jan 30 '15 at 14:58
  • Apparently we have very different concepts of "working". For example, it is rather obvious that because of the aforementioned error vector will contain an extra "stray" element (zero?) at the end. That's an obvious bug, which means that the program is not "working". – AnT stands with Russia Jan 30 '15 at 22:05
  • @AndreyT The problem that I asked about here is solved, and I'm still working on this program, got a new problem, after solving it, I'll ask you to check it. Thanks for caring. – Mohamadreza Jan 30 '15 at 22:14