0

i am currently self-studying c++ and i am currently stuck on this problem.

I want to create a program that save Text(string) and Number(double) loop.

#include <iostream>
#include <iomanip>
#include <cstring>

using namespace std;

int main() {

    string words[999],addwords;
    double numbers[999],addnumbers;
    int totalwords=0,totalnumbers=0;
    head:
        cout << "Word: "; getline(cin,addwords);
        words[totalwords] = addwords;
        totalwords+=1;

        cout << "Numbers: "; cin >> addnumbers;
        numbers[totalnumbers] = addnumbers;
        totalnumbers+=1;
        goto head;
}

EOF

The output must be:

Words: Some Letters
Numbers: 010102
Words: Some Letters
Numbers: 010102
{loop}

The output of this code:

Words: Some Letters
Numbers: 010102
Words: Numbers: 202010

Thank you for help.

dinotom
  • 4,990
  • 16
  • 71
  • 139
Mark Reyes
  • 1
  • 1
  • 2

1 Answers1

0

In modern C++, a better practice is to use the stand-alone getline method, like below:

std::string s;

std::cout << "Enter some text:";
std::getline(cin, s);

If you're going to be doing this in a loop, like above, then I would also recommend using:

std::cin.clear();
std::cin.sync(); 

before each next successive read, so it looks like:

std::cout << "Enter some more text:";
std::cin.clear();
std::cin.sync();
std::getline(cin, s);

There are other issues with your code beyond this, not the least of which is the use of GOTO. What source(s) are you using to learn C++?

Maurice Reeves
  • 1,572
  • 13
  • 19
  • Some sample codes on internet. and this http://www.fredosaurus.com/notes-cpp-2004-12-19.zip i just want to loop without statement so i goto function to make loop easier. (i just want to test add text and numbers on array) can you explain some issues? and how can i fix this? sorry for spoonfeeding. – Mark Reyes Dec 13 '12 at 04:31
  • It's no trouble. I would recommend, however, that you pick up a good book or two on C++. There are copies of the older editions of C++ books out on the web for free that would be a good start (http://www.mindview.net/Books/TICPP/ThinkingInCPP2e.html), and Stroustop's own website has good info. (http://www.stroustrup.com) One other issue I see with your code right now, as it stands above is once you get into your `GOTO` loop you can't exit. That's a problem. – Maurice Reeves Dec 13 '12 at 04:38
  • [StackOverflow C++ book guide](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list?lq=1) – Rob K Apr 21 '16 at 19:38