How to make this program?

I am working on a program that should make learning foreign words easier.
first I typed int with 40 different symbols (so it would be 20 words)
Than i typed "cin>>a1" to begin typing my words, but that code only lets me type numbers. Is there a different approach to this?

1
2
3
4
5
6
7
8
9
10
11
  int main()
{
    int a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,a15,a16,a17,a18,a19,a20,b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,b15,b16,b17,b18,b19,b20;
cout<<"Type in your word" <<endl;
    cin>>a;
    cout<<"Type in foreign word" <<endl;
    cin>>b; 
cout<<"Type in your word" <<endl;
    cin>>a2;
    cout<<"Type in foreign word" <<endl;
    cin>>b2;


plus, I don't know how to make code pick a random "a world"
My idea is to make a code memorise "a word" next to "b word"(foreign word) and it should pick a random "a word" so you can type "b word"
if you are correct the code should move on, if you're not, it should correct you.

My main problem right now is with "cin>>" so I would be happy if someone could explain me what I should do there
> but that code only lets me type numbers. Is there a different approach to this?
Well yes,
1. make your variables a std::string
2. Use an array, not a1,a2,a3....

1
2
3
4
5
6
7
std::string a[20],b[20];
for ( int i = 0 ; i < 20 ; i++ ) {
    cout<<"Type in your word" <<endl;
    cin>>a[i];
    cout<<"Type in foreign word" <<endl;
    cin>>b[i];
}
you can use array for multible variable.
#include<iostream>
using namespace std;
class abc
{
int a[100], b[100],n;
public:
void input()
{
cout<<"enter number:";
cin>>n;
for(i=0;i<n;i++)
{
cout<<"type in your word:";
cin>>a[i];
cout<<"type in goreign word:";
cin>>b[i];
}
}
}
Last edited on
Here I made a small program which can read in a file with word pairs and quizzing for a word randomly.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include <stdexcept>
#include <fstream>
#include <iostream>
#include <random>
#include <string>
#include <utility>
#include <vector>

// Loads a file with word pairs.
//
void load_words( std::vector<std::pair<std::string,std::string>>& wordlist)
{
    std::ifstream ifs( "wordlist.txt");
    if( !ifs )
    {
        throw std::runtime_error("file wordlist.txt couldn't opened!");
    }
    wordlist.clear();
    std::string word_a, word_b;
    while( ifs >> word_a >> word_b)
    {
        wordlist.push_back( std::make_pair(word_a, word_b));
    }        
}

// Prints all word pairs
//
void print_words( const std::vector<std::pair<std::string,std::string>>& wordlist)
{
    for ( auto wordpair : wordlist)
    {
        std::cout << wordpair.first << ' ' << wordpair.second << '\n';
    }
}

// Compares a a word from word list.
//
void process_word(
    const std::vector<std::pair<std::string,std::string>>& wordlist,
    const std::string& searched,
    std::string& guessed
)
{
    for( auto wordpair : wordlist)
    {
        if (wordpair.first == searched)
        {
            if (wordpair.second == guessed)
            {
                std::cout << "You're right.\n";
            }
            else
            {
                std::cout << "You failed, the right word is " << wordpair.second << '\n';
            }
        }
    }
}

int main()
{
    std::vector<std::pair<std::string,std::string>> wordlist;
    load_words(wordlist);
    print_words(wordlist);
    
    // Initializes the random stuff
    std::random_device rd;  // Generator
    std::uniform_int_distribution<> uid(0,wordlist.size()-1);
    
    while (true)
    {
        std::pair<std::string,std::string> p = wordlist[uid(rd)];
        std::cout << "What word means " << p.first << " in German?\n";
        std::string guessed;
        std::cin >> guessed;
        if (guessed == "quit" || guessed == "exit")
            return 0;
        process_word(wordlist, p.first, guessed);
        std::cout << '\n';
    }
}

Don't be worry if you don't understand some pieces of the program. Rather consider this example as a starting point for researching some of the facilities which C++ provides.
Last edited on
Topic archived. No new replies allowed.