2017-08-27 2 views
-4

Ich habe folgenden Codestore der Wert von cout

#include <iostream> 
#include <math.h> 
#include <string> 
#include <sstream> 
using namespace std; 
int main() { 
int value 
cout << "Input your number: " << endl; 
cin >> value; 
string s = to_string(value); 
const int count = s.length(); 
int position = count; 
for (int i = 1; i < count + 1; i++) 
{ 
    int pwr = pow(10, position - 1); 
    cout << ((value/pwr) + position) % 10; 
    position--; 
    value = value % pwr; 
} 

Statt cout, wie ich den Wert ((value/pwr) + position) % 10 in eine Variable speichern kann, die für die Schleife. Vielen Dank für die Hilfe.

[Bearbeiten] ich stattdessen ein Array hinzugefügt

int val[7]; 
int position = count; 
for (int i = 1; i < count + 1; i++) 
{ 
    int pwr = pow(10, position - 1); 
    val[i-1] = ((value/pwr) + position) % 10; 
    position--; 
    value = value % pwr; 
} 
cout << "Encoded value is: "; 
for (int i = 0; i < 8; i++) 
{ 
    cout << val[i]; 
} 

Es war in der Lage die Werte ich will, aber es gibt ein Run-Time Failure # 2 Ausgang - stapelt um die Variable 'val' wurde beschädigt . Warum das?

+0

nur einen Vektor erstellen und Ihre Ergebnisse –

+0

zurückzudrängen Kann nicht Codes verwenden wir nicht –

+0

@ Wing Hang Khoo in der Klasse lernen haben. http://en.cppreference.com/w/cpp/container/vector –

Antwort

0

Sie können einen STL-Container wie Vektor verwenden.

#include <iostream> 
#include <math.h> 
#include <string> 
#include <sstream> 
#include <vector> 

using namespace std; 
int main() { 
    int value; 
    cout << "Input your number: " << endl; 
    cin >> value; 
    string s = to_string(value); 
    const int count = s.length(); 
    int position = count; 

    vector<int> outputs; 

    for (int i = 1; i < count + 1; i++) 
    { 
    int pwr = pow(10, position - 1); 
    outputs.push_back(((value/pwr) + position) % 10); 
    cout << outputs.back(); 

    position--; 
    value = value % pwr; 
    } 

    cout << endl; 

    // iterate through the vector later 
    for (auto i : outputs) 
    { 
    cout << i; 
    } 

    cin.get(); 
} 
+0

Danke dafür, aber kann keine Codes verwenden, die wir im Unterricht nicht gelernt haben. –

0

Store Vektor und dann drucken Sie es

Es ist nicht so schwierig .Wie Sie wissen nicht, was die Größe sein wird oder wie viele Zahlen der Folge wird es es nützlich ist, Vektor zu verwenden.

Referenz ist hier http://en.cppreference.com/w/cpp/container/vector

#include <iostream> 
#include <math.h> 
#include <string> 
#include <sstream> 
#include<vector> 
#include<iterator> 
#include<algorithm> 

using namespace std; 

int main() { 
    int value; 
    vector<int> results; 
    cout << "Input your number: " << endl; 
    cin >> value; 
    string s = to_string(value); 
    const int count = s.length(); 
    int position = count; 
    for (int i = 1; i < count + 1; i++) 
    { 
     int pwr = pow(10, position - 1); 
     auto val = ((value/pwr) + position) % 10; 
     results.push_back(val); 
     position--; 
     value = value % pwr; 
    } 
    copy(results.begin(),results.end(),ostream_iterator<int>(cout," ")); 

} 

Ausgabe

Input your number: 
12 
3 3 Program ended with exit code: 0