2017-12-27 12 views
-3

タイトルでは、現在、charの配列からInt変数に値を追加する方法が混乱しています。私はPythonでこれを回避する方法を知っていますが、私は本当にC++には新しく、私は解決策をオンラインで探すことを試みてきましたが、そこには何もありません。C++ /// forループとifループを使用してint変数に値を追加するにはどうすればいいですか?

ので、とにかくここに私のコード

#include "stdafx.h" 
#include <iostream> 
#include <string> 
#include <sstream> 
#include <vector> 
#include <algorithm> 
#include <iterator> 
using namespace std; 


int main() 
{ 
    char studentId[9]; 
    cout << "Enter Student ID : "; 
    cin >> studentId 

    int n; 
    cout << "\nYour special number is "; 
    for (n=1; n<(sizeof(studentId)); n+=2) 
    { 
     cout << studentId[n]; //this displays all numbers in even places, this one works 
    } 


    char oddId[8]; 
    for (int c = 0; c < 8; c++) 
    { 
     if (studentId[c] % 2 != 0) 
     { 
      oddId[c] = studentId[c]; 
     } 
    } 
    cout << "\nOdd numbers after the for loop : " << oddId; 
    int oddInt = stoi(oddId); 
    cout << "\nOdd numbers after converted to Int : " << oddInt; // i need to convert the odd numbers to int so i can get the remainder 
    cout << "\nYour lucky number is " << oddInt % 9 << endl; //remainder of odd numbers when divided by 9 


    return 0; 
} 

だと、これは出力

output

で、事前にどうもありがとう今3日間これを理解しようとして。

[編集]

これは私がPythonで何をしたか、混乱かもしれないですが、それは私がそれになりたい方法を動作します:

def magic(num_list): 
s = ''.join(map(str, num_list)) 
return(int(s)) 

std_id = input("Enter Student ID: ") 

id_list = [] 
for a in std_id: 
    id_list.append(a) 

id_no = list(map(int, id_list)) 

even_list = id_no[1::2] 

odd_list = [] 
for x in id_no: 
    if x % 2 == 1: 
     odd_list.append(x) 

oddodd = magic(odd_list) 
eveneven = magic(even_list) 

print("Your special number is", eveneven) 
print("Your lucky number is", oddodd % 9) 
+0

"* charの配列の値をInt変数*に追加するという意味は理解できません。あなたは数字だけを含む 'char 'を' int'変数に変換しますか? – Fureeish

+0

申し訳ありませんが、私はまだプログラミング用語を学んでいます。基本的には、ええ、私は私の "oddId"(char)をint変数に変換したいので、 "%"演算子を使用することができます –

+0

少なくともterminologiesであなたが求めているものは切り離されているようです。 Pythonのコードは、あなたが望むことをするためにどのように見えますか?そのコードを表示すると、私はそれが人々の反応にもっと感謝するだろうと思う。ありがとう! –

答えて

2

コードのこの部分にはいくつかの問題があります。

char oddId[8]; 
for (int c = 0; c < 8; c++) 
{ 
    if (studentId[c] % 2 != 0) 
    { 
     oddId[c] = studentId[c]; 
    } 
} 

まず、oddIdは十分な大きさではありません。 8桁の文字列を保持するには、ヌルターミネータが必要なため、9個の要素が必要です。

第2に、入力配列と出力配列に同じインデックスcを使用しています。これは、studentIdの偶数要素に対応するoddIdの要素が決して埋められないことを意味します。そのため、出力の途中にあるすべてのガベージ文字が表示されます。

第3に、終了時にヌルターミネーターを追加しないでください。そのため、出力の最後にガベージ文字が多く表示されます。

第4に、入力文字列が8文字未満の場合、最後の文字列を読み込みます。制限を取得するにはstrlen()を使用する必要があります。また、前のループでこれを使用して、sizeof(studentId)の代わりにすべての偶数の場所を出力する必要があります。

はこれを試してください:あなたの代わりにCスタイルの文字列のstd::stringを使用した場合、それは連結をサポートするので

ところで
char oddId[9]; 
int indexout = 0; 
int len = strlen(studentId); 
for (int indexin = 0; indexin < len; indexin++) 
{ 
    if (studentId[indexin] % 2 != 0) 
    { 
     oddId[indexout++] = studentId[indexin]; 
    } 
} 
oddId[indexout] = '\0'; 

は、物事は、非常に容易になるだろう。

+0

あなたはたぶん "for文"の中に "indexin ++"とタイプすることを意図していましたか?私はちょうどそれをし、それは働いた!どうもありがとうございます!私はこの速い応答を期待していませんでした^^ –

+0

私はまだあなたのコードがどのように動作するかを理解しようとしています。私はこの "Cスタイルの文字列" –

0
int value = 0; 
for(int ndx = 0; count != ndx; ++ndx) 
{ 
    value *= 10; 
    value += str[ndx] - '0'; 
} 

が長さがすでに知られていると仮定し、すべての文字が0〜9の間であること。

関連する問題