タイトルでは、現在、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;
}
だと、これは出力
で、事前にどうもありがとう今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)
"* charの配列の値をInt変数*に追加するという意味は理解できません。あなたは数字だけを含む 'char 'を' int'変数に変換しますか? – Fureeish
申し訳ありませんが、私はまだプログラミング用語を学んでいます。基本的には、ええ、私は私の "oddId"(char)をint変数に変換したいので、 "%"演算子を使用することができます –
少なくともterminologiesであなたが求めているものは切り離されているようです。 Pythonのコードは、あなたが望むことをするためにどのように見えますか?そのコードを表示すると、私はそれが人々の反応にもっと感謝するだろうと思う。ありがとう! –